UITableView单元格保持选中状态

UITableView单元格保持选中状态

本文介绍了iPhone UITableView单元格保持选中状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的UITableView有时细胞保持选择后触摸。
因为它偶尔会发生,所以我不能重现
的问题。



任何提示?也许它与无效的释放tableView有关系。

   - (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

switch(row){
case 0:
FruitViewController * fruitController = [FruitViewController alloc];
[fruitController retain];
[fruitController initWithNibName:@FruitViewbundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:fruitController animated:YES];
[fruitController release];
break;
case 1:
CerealsViewController * cerealsController = [CerealsViewController alloc];
[cerealsController retain];
[cerealsController initWithNibName:@CerealsViewbundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:cerealsController animated:YES];
[cerealsController release];
break;
默认值:
break;
}
}


解决方案

I无法告诉您为什么会看到此问题,但这里有一些修复问题的建议:



根据Apple HIG,选择不应该消失,直到返回从视图控制器刚推入堆栈。如果你的控制器只是一个UITableViewController,它应该在返回视图时自动取消选择。如果没有,添加

   - (void)viewWillAppear:(BOOL)animated {
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow ] animated:animated];
[super viewWillAppear:animated]
}

视图控制器中的某处。



如果有任何行,当点击时,不要去另一个视图,并且事实上没有做任何选择时,它们不应该是可选择的,因此您可以覆盖

   - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 



并在不应选择该行的情况下返回 nil b $ b

in my UITableView sometimes cells stay selected after touching. Because it happens only occasionally, I'm not able to reproduce the problem.

Any hints? Maybe it has something to do with unproper releasing of tableView?

    - (void)tableView:(UITableView *)tableView
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUInteger row = [indexPath row];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

switch (row) {
    case 0:
        FruitViewController *fruitController = [FruitViewController alloc];
        [fruitController retain];
        [fruitController initWithNibName:@"FruitView" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:fruitController animated:YES];
        [fruitController release];
        break;
    case 1:
        CerealsViewController *cerealsController = [CerealsViewController alloc];
        [cerealsController retain];
        [cerealsController initWithNibName:@"CerealsView" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:cerealsController animated:YES];
        [cerealsController release];
        break;
    default:
        break;
}
   }
解决方案

I can't tell you why you're seeing the issue, but here are some suggestions for fixing it:

According to the Apple HIG, the selection should not disappear until returning from the view controller just pushed onto the stack. If your controller is just a UITableViewController, it should deselect automatically upon returning to the view. If not, add

- (void) viewWillAppear:(BOOL)animated {
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated];
    [super viewWillAppear:animated];
}

somewhere in the view controller.

If there are any rows that, when clicked, do not go to another view, and don't in fact do anything when selected, they shouldn't be selectable, so you can override that in

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

and return nil in the cases that the row should not be selected.

这篇关于iPhone UITableView单元格保持选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:48