首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从UITableView中删除并支持iOS 4中的数据源

从UITableView中删除并支持iOS 4中的数据源
EN

Stack Overflow用户
提问于 2011-06-23 17:31:13
回答 1查看 1K关注 0票数 1

在阅读了苹果的文档并搜索了谷歌之后,我对此感到有点困惑。问题的要点是,我知道我需要从UITableView和数据源中删除(在本例中是NSMutableArray),但是不管我按照什么顺序执行,它都不会让我这样做。

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {        
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [myMutableObjectList removeObjectAtIndex:indexPath.row];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // not used
    }   
}

无论我为这两种情况使用哪种顺序:

代码语言:javascript
复制
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationFade];
     [myMutableObjectList removeObjectAtIndex:indexPath.row];

它要么抱怨还剩多少个元素,要么说删除没有发生。

删除前的

“(计数)必须在删除后匹配(计数)减去许多您已删除的0插入1删除”

和tableView..。首先,它说计数是相同的,1删除要发生。

和myMutableObjectList..。首先,它说计数是少1(很好!)但是0删除想要发生。

如果我不能这样做,我该怎么办?

更新:

代码语言:javascript
复制
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   
   if([myMutableObjectList count] == 0) {
      return section; 
   } else {
    return 4; //4 variables in myObject
   }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{   
    NSLog(@"Size of list: %d", [myMutableListList count]);
    if([myMutableList count] == 0) {
        return 100;
    } else {
        return [myMutableList count];
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-24 13:51:55

你的numberOfRowsInSection不符合你想要的行为。它永远不会反映对数据存储的更改。

代码语言:javascript
复制
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   
   if([myMutableObjectList count] == 0) {
      return section; 
   } else {
    return 4; //4 variables in myObject
   }
}

你为什么要在这里回来。我想你想要回[myMutableObjectList count]

此方法需要反映所有单元格的当前计数。在执行删除操作时,可以从myMutableObjectList中删除一个对象,但该更改不会影响numberOfRows方法的结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6458198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档