我有一个UITableView,我使用以下代码扩展/收缩单元格。
我保存最后2个indexPaths以对其执行reloadRowsAtIndexPaths:
。
现在我在第0部分的标题中添加了UISearchBar
。如果我选中了searchBar,则在UITableView的顶部会显示一个KeyBoard - 到目前为止一直很好。
但我希望用户能够触摸单元格并禁用KeyBoard。为此,我测试搜索框是否是-tableView:didSelectRowAtIndexPath:
但这样做会导致行标记1,2,3中的“SIGKILL”
我真的不明白为什么
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Article *article = [articles objectAtIndex:indexPath.row];
ArticleCell *cell = (ArticleCell*)[self.tableView dequeueReusableCellWithIdentifier:@"articelcell"];
if (cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"ExtendibleCell" owner:self options:nil] objectAtIndex:0];
}
//....
cell.articleName.text = [NSString stringWithFormat:@"%@",article.name ];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([searchBar isFirstResponder]) {
[searchBar resignFirstResponder];
}
[orderTableDelegate receiveSelectedArticleName:[[articles objectAtIndex:indexPath.row] name]];
firstSelected = lastSelected;
lastSelected = indexPath;
if (lastSelected == firstSelected && firstSelected != nil) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:lastSelected] withRowAnimation:CELL_ANIMATION]; //1
lastSelected = nil;
firstSelected = nil;
return;
}
if (lastSelected != nil) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:CELL_ANIMATION];//2
}
if (firstSelected != nil) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:firstSelected] withRowAnimation:CELL_ANIMATION];//3
}
}
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section ==0) {
if (searchBar == nil) {
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[searchBar setShowsBookmarkButton:YES];
[searchBar setKeyboardType:UIKeyboardTypeAlphabet];
[searchBar setBarStyle:UIBarStyleBlack];
[searchBar setShowsCancelButton:YES];
[searchBar setDelegate:self];
}
return searchBar;
}
return nil;
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:lastSelected] && lastSelected!=firstSelected) {
return [[(Article *)[articles objectAtIndex:indexPath.row] sizesAndPrices] count]*PACKAGESIZE_PRICE_BUTTON_HEIGHT +30;
}
return 40.0;
}
修改
我清理了-tableView:didSelectRowAtIndexPath:
的代码,但问题保持不变
@property(nonatomic,retain) NSIndexPath *firstSelected;
@property(nonatomic,retain) NSIndexPath *lastSelected;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[orderTableDelegate receiveSelectedArticleName:[[articles objectAtIndex:indexPath.row] name]];
self.firstSelected = nil;
self.firstSelected = self.lastSelected;
self.lastSelected = nil;
self.lastSelected = [indexPath retain];
if (self.firstSelected == self.lastSelected) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.firstSelected] withRowAnimation:CELL_ANIMATION];
[self.firstSelected release];
[self.lastSelected release];
self.firstSelected = nil ;
self.lastSelected = nil ;
} else {
if (self.firstSelected != nil) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.firstSelected] withRowAnimation:CELL_ANIMATION];
}
if (self.lastSelected != nil) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.lastSelected] withRowAnimation:CELL_ANIMATION];
}
}
if ([searchBar isFirstResponder]) {
[searchBar resignFirstResponder];
}
}
答案 0 :(得分:0)
firstSelected = lastSelected; lastSelected = indexPath;
这让我相信lastSelected是一个实例变量?如果是这种情况,则表示您没有正确保留它,并且无法保证它仍然存在,超出了此方法的范围。如果要在执行后使用它,则需要保留在didSelectRowAtIndexPath:参数中传递的indexPath。
请记住,如果你这样做,你需要在整个方法中更好的内存管理...即。在更改它的值或将其设置为nil之前释放lastSelected。
假设firstSelected和lastSelected是实例变量,你可以这样做。 (所有释放和!=检查都会消失,如果你使它们保留属性并使用了setter)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([searchBar isFirstResponder]) {
[searchBar resignFirstResponder];
}
[orderTableDelegate receiveSelectedArticleName:[[articles objectAtIndex:indexPath.row] name]];
if (firstSelected != lastSelected) {
[firstSelected release];
firstSelected = [lastSelected retain];
}
if (lastSelected != lastSelected) {
[lastSelected release];
lastSelected = [indexPath retain];
}
if (lastSelected == firstSelected && firstSelected != nil) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:lastSelected] withRowAnimation:CELL_ANIMATION]; //1
[lastSelected release];
lastSelected = nil;
[firstSelected release];
firstSelected = nil;
return;
}
if (lastSelected != nil) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:CELL_ANIMATION];//2
}
if (firstSelected != nil) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:firstSelected] withRowAnimation:CELL_ANIMATION];//3
}
}