2013-05-22 46 views
1

我有一个动态表,用户可以在其中添加&删除数据。该表格显示购物清单。如果购物完成,用户应该能够勾选需要的项目,并且应该能够取消勾选,我已通过设置accessorybutton来实现此目的。但是,问题出在我从中删除一行时,单元格被删除,但附加到该单元格的附加按钮保持相同状态。在ios中维护辅助视图的状态

- (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; 
    } 
} 
+1

PLS显示我们的代码 – manujmv

+0

' - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {cell = [tableView cellForRowAtIndexPath:indexPath]; 如果(cell.accessoryView ==无) { cell.accessoryView = [[ALLOC的UIImageView] initWithImage:[UIImage的imageNamed:@ “tick_btn”]]; } else { cell.accessoryView = nil; } }' – JgdGuy

回答

0

因为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,以确保一切正确更新。

+0

好吧,没有检查购物是否完成的方法,如果用户完成购物清单中提到的任务,用户可以选择打勾。 我的意思是我不能确定要显示的刻度,它需要显示在行被选中。 – JgdGuy

+0

那么什么是跟踪你的数据? GUI本身? – hsoi

+0

用户可以在他的列表中添加n个删除随机事物。我已经完成了购物,他/她可以打个勾。 – JgdGuy

0

你需要跟踪所选项目

- (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]; 
    } 

} 

的编辑

​​
+0

什么是checkedIndexPath? – JgdGuy

+0

只是一个NSMutaleArray存储selectedItems的indexPath –

+0

不帮助:(试过..! – JgdGuy