【问题标题】:Updating Table when inserting/deleting rows插入/删除行时更新表
【发布时间】:2010-10-05 11:00:07
【问题描述】:

我有一个单例类,用于在各种视图中显示数据。

有一个 TableView 用于删除/插入行。我有一个在编辑/完成之间切换的按钮 允许编辑。 'streams 是 Singleton 类中的一个变量'

- (void)setEditing:(BOOL)flag animated:(BOOL)animated{   

       int count = [streams count];


    UITableView *tableView = (UITableView *)self.view;
    NSArray *topIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]];

 if (self.editing == YES)
 {NSLog(@"EDITING");
 [tableView insertRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom];}

 else{NSLog(@"NOT EDITING");
 [tableView deleteRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom];}


}

并使用editingStyleForRowAtIndexPath 允许选择每行使用哪种编辑样式。

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

      int count = [streams count];
         int row = indexPath.row ;

 if (row == count)

       return    UITableViewCellEditingStyleInsert;

   else

       return UITableViewCellEditingStyleDelete;}

在编辑模式下,我使用 cellForRowAtIndexPath 创建一个带有文本“添加电话号码”的附加行。

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

static NSString *DeleteMeCellIdentifier = @"AudioCellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                         DeleteMeCellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:DeleteMeCellIdentifier] autorelease];
}
int x = indexPath.row;
if (self.editing == YES)
{
    if (indexPath.row == [streams count])

        cell.textLabel.text = @"Add Phone Number";

    else
        cell.textLabel.text = [self.streams objectAtIndex:indexPath.row];
}
else
{
    cell.textLabel.text = [self.streams objectAtIndex:indexPath.row];
}

return cell;}

删除行工作正常。在选择插入一行时,另一个视图被推入堆栈。从这个视图中,用户可以获得许多文本选项来标记我们刚刚创建的新行。以下代码用于在选择完成后更新单例类。

- (void)viewWillDisappear:(BOOL)animated {

Disc *disc = [Disc sharedDisc];
[disc.streams insertObject:@"0208" atIndex:0];
[super viewDidAppear:animated];}

一旦选择了行的文本,用户必须选择后退按钮才能返回上一个 TableView。这就是问题出现的时候。在主 TableView 中,而不是一个标记为“添加电话号码”的选项 那里有两个。

我知道 TableView 正在使用的 singelton 类已经更新,它是 TableView 没有以正确的方式更新。如果我在 Edit/Done 之间切换,tableView 会正确显示信息。

我尝试在 ViewDidLoad 和 ViewWillAppear 方法中更新单例类,但结果是一样的,第一次重新加载视图时,它没有正确显示新行。

我考虑过覆盖“返回”BarButton 以尝试让 TableView 正确显示。

【问题讨论】:

    标签: sql iphone sql-insert delete-row


    【解决方案1】:

    我不太明白为什么重复的行与更新表格视图有关。但这听起来有点像您正在创建两个表,不知何故?甚至是两个视图控制器,但我假设您的导航代码设置正确(使用标签栏可能会导致视图控制器初始化代码不正确)。

    您肯定想要更改 ViewWillAppear 中表格内容的任何内容。

    即使在 ViewDidLoad 中更新(通常仅在第一次出现视图时调用)时,您是否也会得到重复的单元格。要在返回时执行,

    如果您要刷新表格数据,请在 ViewWillAppear 中使用 [tableview reloadData]。

    如果一切都失败了,要跟踪这个错误,我建议在 tableView 对象上添加表达式并观察它的变化(在调试模式下)。

    【讨论】:

    • 谢谢亨利克。无论使用 ViewDidLoad 还是 ViewWillAppear,都会出现重复项。我应该提到是的,使用了两个 ViewController。如果我编辑代码以便添加一行不会推送新视图,而是更新 Singeton 类,然后调用 [tableView Reload data] 它可以完美运行。从第二个 tableView 更新 singelton 并导航回第一个 tableView 时会出现问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多