【问题标题】:Maintain state of Accessory view in ios在ios中维护附件视图的状态
【发布时间】:2013-05-22 10:38:11
【问题描述】:

我有一个动态表,用户可以在其中添加和删除数据。该表显示购物清单。如果购物完成,用户应该能够勾选所需的项目并且也应该能够取消勾选,我通过设置附件按钮来实现这一点。但是,当我从中删除一行时,问题就来了,单元格被删除,但是附加到该单元格的附件按钮保持在相同的状态。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
  cell = [tableView cellForRowAtIndexPath:indexPath]; 
   if (cell.accessoryView == nil)
   {    
    cell.accessoryView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick_btn"]]; } else { cell.accessoryView = nil; 
   }
}

【问题讨论】:

  • - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryView == nil) { cell.accessoryView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick_btn"]]; } else { cell.accessoryView = nil; } }

标签: ios uitableview uinavigationitem accessoryview


【解决方案1】:

因为UITableView 通常重用UITableViewCell 的实例,所以您必须确保您的'-tableView:cellForRowAtIndexPath: 方法正确设置单元格的所有属性。其他陈旧的数据可能会持续存在。我猜这可能是您的问题,没有完整地查看您的代码。

所以,是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString*    cellIdentifier = @"TheCellIdentifier";
    UITableViewCell*    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    ShoppingObject* shopping = [self.myShoppingList objectAtIndex:indexPath.row];
    UIImageView*    accessoryView = nil;

    if (shopping.isDone) {
        accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick_btn"]];
    }

    cell.accessoryView =  accessoryView;

    return cell;
}

它通过重用缓存或创建一个新缓存来获取单元。然后它会检查您的数据模型的状态,以查看该行中表示的对象是否已完成购物,如果购物已完成,则会为您提供图像。请注意,购物还没有完成,没有创建 accessoryView,因此无论 ShoppingObject 的状态在该表行中表示什么,该单元格的 accessoryView 都将正确设置。

所以我可能会在你的-tableView:didSelectRowAtIndexPath: 中做的只是简单地将-reloadData 放在桌子上以确保所有内容都正确更新。

【讨论】:

  • 好吧,没有一种方法可以检查购物是否完成,如果他/她完成了购物清单中提到的任务,用户可以选择打勾。我的意思是我无法确定要显示的刻度,它需要在选中行时显示。
  • 那么什么是跟踪您的数据? GUI 本身?
  • 用户可以在他的列表中添加 n 删除随机的东西。我他已经完成了购物,他/她应该能够在上面打勾。
  • 这很好,但是什么会跟踪列表中的内容? UITableView GUI 本身?你有某种数据模型吗?我会假设您这样做,因为我认为您允许他们输入类似任务名称的内容。为什么该对象不跟踪其余的数据状态,例如任务是否完成?无论如何,重点仍然存在:您需要确保 cell.accessoryView 始终在 -tableView:cellForRowAtIndexPath: 中进行相应设置,因为很可能该单元格正在被重用,并且该特定单元格实例具有您没有清除的“陈旧”数据.
  • 我如何删除这个“陈旧的单元格”
【解决方案2】:

您需要跟踪所选项目

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
  cell = [tableView cellForRowAtIndexPath:indexPath]; 
   if (cell.accessoryView == nil)
   {    
    cell.accessoryView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick_btn"]]; 
    [self.checkedIndexPaths addObject:indexPath];
   } 
   else { 
   cell.accessoryView = nil; 
   [self.checkedIndexPaths removeObject:indexPath];
   }

}   

编辑

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

// Do other stuffs

cell.accessoryView = nil;

if ([self.checkedIndexPath containsObject:indexPath]) {
   cell.accessoryView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick_btn"]]; 
  }  

}

【讨论】:

  • checkIndexPath 是什么?
  • 只是一个 NSMutaleArray 来存储 selectedItems 的 indexPath
  • 在for循环之前..将单元格附件视图设置为nil.. cell.accessoryView = nil
  • @anil...尝试使用这个...但是当我删除一行时,所有附件视图都变为 nil..!
  • 你如何声明checkedIndexPath数组..检查数组是否正在释放?当你删除一行 cellForIndexPath 会调用对吗?在accessoryView检查部分放一个断点并进行检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
相关资源
最近更新 更多