2011-06-14 66 views
1

我有一个tableview,我需要每次选择一行时显示一个复选标记。 (多选)我的代码如下。我也能够取消选择一行。问题:正在重用多个选择tableview单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *RootViewControllerCell = @"RootViewControllerCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RootViewControllerCell]; 

if(nil == cell) 
{ 
    cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:RootViewControllerCell] autorelease]; 

} 
cell.textLabel.font = [UIFont fontWithName:[NSString stringWithFormat:@"HelveticaNeue-Bold"] size:12]; 
textView.textColor = [UIColor colorWithRed:0.281 green:0.731 blue:0.8789 alpha:1]; 
cell.textLabel.text = [optionsArray objectAtIndex:[indexPath row]]; 
if (pathIndex == indexPath) 
{ 
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    else { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
} 
return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
pathIndex = indexPath; 
[surveytableView reloadData]; 
} 

但我有一个单元格被重用的问题。当我选择一个单元另一个单元其他地方也被选中。只有复选标记(或没有复选标记)才会被重用,其他细节(如行标题等)不会被重用。任何解决方案来解决这个提前致谢。

+0

我发现从堆栈溢出回答另一个问题一个解决方案: http://stackoverflow.com/questions/6023883/ uitableview-multiple-checkmark-selection Folllow this above link。似乎现在对我来说工作得很好。 – 2011-06-14 07:33:29

回答

0

在你

if (pathIndex == indexPath) 

你比较指针不是他们的价值观,尝试

[pathIndex isEqual:indexPath] 

或使用

- (NSComparisonResult)compare:(NSIndexPath *)otherObject; 

接下来你将值分配给pathIndex没有保留或复制它像

pathIndex = [indexPath copy]; 

(当然现在因为你保留的价值,复制,你必须释放前一个[pathIndex发行]新对象之前;)

最后,没有多重选择由您的实现提供,只有单一的选择。您可以尝试添加NSIndexPath对象并将其移除到NSMutableArray,然后检查它们在cellForRowAtIndexPath中的可变数组中是否存在。

0

问题是,如果当前行与pathIndex ....匹配,那么您只对附件执行某些操作......那么如果它是一个正常的单元格...?你永远不会回头。你想...

cell.accessoryType = UITableViewCellAccessoryNone; 

if ([pathIndex isEqual:indexPath]) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

在设置任何特定属性之前重置单元是一种很好的做法。

2

将它添加到cellForRowAtIndexPath

if ([tableView.indexPathsForSelectedRows containsObject:indexPath]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else if (![tableView.indexPathsForSelectedRows containsObject:indexPath]) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

为我工作

相关问题