0

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

有一个 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 没有以正确的方式更新。如果我然后在编辑/完成之间切换,tableView 会正确显示信息。

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

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

4

1 回答 1

0

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

你肯定想要任何改变 ViewWillAppear 中表格内容的东西。

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

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

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

于 2010-10-05T11:18:36.067 回答