【发布时间】: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