我有一个单独的类用于在各种视图中显示数据。插入/删除行时更新表格
有一个TableView用于删除/插入行。我有一个按钮在Edit/Done 之间变化以允许编辑。 “流是Singleton类中的变量”
并使用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方法中的单例类,但结果相同,第一次重新加载视图时,它不会正确显示新行。
我想过重写“Back”BarButton来尝试让TableView正确显示。
谢谢Henrik。无论使用ViewDidLoad还是ViewWillAppear,重复都会出现。我应该提到的是,有两个ViewController使用。如果我编辑代码,以便添加一行不会推新视图,而是更新Singeton类,然后调用[tableView Reload data],它可以很好地工作。从第二个tableView更新singelton并导航回第一个tableView时出现问题。 – user244295 2010-10-05 11:25:53