微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

问题:多个选择表视图单元正在重用

如何解决问题:多个选择表视图单元正在重用

| 我有一个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];
}
但是我对单元的重用有疑问。当我选择一个单元格时,也会选择其他位置的另一个单元格。仅复选标记(或无复选标记)被重用,其他详细信息(如行标题等)也不会被重用。任何解决此问题的解决方案。提前致谢。     

解决方法

        将其添加到
cellForRowAtIndexPath
 if ([tableView.indexPathsForSelectedRows containsObject:indexPath]) {
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
 } else if (![tableView.indexPathsForSelectedRows containsObject:indexPath]) {
     cell.accessoryType = UITableViewCellAccessoryNone;
 }
为我工作     ,        在你的
if (pathIndex == indexPath)
您正在比较指针而不是指针的值,请尝试
[pathIndex isEqual:indexPath]
或使用
- (NSComparisonResult)compare:(NSIndexPath *)otherObject;
接下来,您要为pathIndex分配一个值,而不保留或复制它
pathIndex = [indexPath copy];
(当然,现在由于保留了该值,因此在复制新对象之前,您必须释放前一个[pathIndex release];) 最后,您的实现没有提供多个选择,只有单个选择。您可以尝试将NSIndexPath对象添加到NSMutableArray并将其删除,然后在cellForRowAtIndexPath处检查它们是否存在于可变数组中。     ,        问题是,仅当当前行与pathIndex匹配时,才对附件执行某些操作。...如果它是普通单元格,该怎么办...?您永远不会将它重新设置。你要...
cell.accessoryType = UITableViewCellAccessoryNone;

if ([pathIndex isEqual:indexPath])
{
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
在设置任何特定属性之前,最好重置单元格。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。