我有一个单例类,用于在各种视图中显示数据。
有一个 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 正确显示。