在ios中维护附件视图的状态
我有一个动态表,用户可以在其中添加和删除数据。该表显示了购物清单。如果购物完成,用户应该能够勾选所需的项目,也应该能够取消勾选,我通过设置accessorybutton来实现这一点。但是,当我从中删除一行时,问题就来了。单元格被删除,但是附加到该单元格的辅助按钮保持不变在ios中维护附件视图的状态,ios,uitableview,uinavigationitem,accessoryview,Ios,Uitableview,Uinavigationitem,Accessoryview,我有一个动态表,用户可以在其中添加和删除数据。该表显示了购物清单。如果购物完成,用户应该能够勾选所需的项目,也应该能够取消勾选,我通过设置accessorybutton来实现这一点。但是,当我从中删除一行时,问题就来了。单元格被删除,但是附加到该单元格的辅助按钮保持不变 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { cell = [tableVie
- (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;
}
}
由于
UITableView
通常重用UITableView单元格的实例,因此必须确保“-tableView:cellforrowatinexpath:
方法正确设置单元格的所有属性。否则陈旧的数据可能会持续存在。我猜这可能是您的问题,您的代码缺乏完整的外观
比如说:
- (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
,以确保所有内容都正确更新。您需要跟踪所选项目
- (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"]];
}
}
-(void)tableView:(UITableView*)tableView DidSelectRowatineXpath:(NSIndexPath*)indexPath{cell=[tableView CellForRowatineXpath:indexPath];if(cell.accessoryView==nil){cell.accessoryView=[UIImageView alloc]initWithImage:[UIImageNamed:@“tick_btn”]}else{cell.accessoryView=nil;}
没有一种方法可以检查购物是否完成,如果用户完成了购物列表中提到的任务,用户可以选择打勾。我的意思是,我无法确定要显示的勾号,它需要在选定行时显示。那么,什么是跟踪您的数据?GUI本身?用户可以在列表中添加或删除随机的内容。如果他/她已经完成了购物,他/她应该能够在上面打勾。那很好,但是什么可以跟踪列表中的东西呢?UITableView GUI本身?你有某种数据模型吗?我想你会的,因为我认为你允许他们输入任务名称之类的内容。为什么该对象不能跟踪其余的数据状态,比如任务是否完成?无论如何,重点仍然是:您需要确保在-tableView:cellForRowAtIndexPath:中始终相应地设置cell.accessoryView,因为该单元格很可能正在被重用,并且特定的单元格实例具有您不清除的“过时”数据。如何删除此“过时”单元格什么是checkedIndexPath?只是一个NSMutaleArray,用于在for循环之前存储SelectedItemsB的indexPath。。将单元附件视图设置为零。。cell.accessoryView=nil@anil...tried使用此…但我删除了一行所有附件视图变为零。。!如何声明checkedIndexPath数组。。检查阵列是否正在释放?删除行时,cellForIndexPath将被调用,对吗?在accessoryView检查部分中设置断点并进行检查