插入/删除行时更新表

发布于 2024-09-26 07:23:14 字数 2575 浏览 5 评论 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 正在使用的 singleton 类已更新,这是 TableView 未以正确的方式更新。如果我随后在“编辑”/“完成”之间切换,则 tableView 会正确显示信息。

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

我考虑过重写“后退”BarButton 来尝试让 TableView 正确显示。

I have a singleton class which is used to display data across various views.

There is one TableView which is used to delete/insert rows. I have a button that changes between Edit/Done
to allow Editing. 'streams is a variable within the Singleton class'

- (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];}


}

And use editingStyleForRowAtIndexPath to allow choose which editing style is used for each row.

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

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

 if (row == count)

       return    UITableViewCellEditingStyleInsert;

   else

       return UITableViewCellEditingStyleDelete;}

I use cellForRowAtIndexPath for to create an additional row with the text " Add phone Number" when in edit mode.

- (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;}

Deleting Rows works fine. On selecting to insert a row another view is pushed onto the stack. From this view the user is given a number of text options to label the new row we've just created. The following code is used to update the singleton class once a selection has been made.

- (void)viewWillDisappear:(BOOL)animated {

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

Once the text for the row has been selected to user has to select the back button to get back to the previous TableView. Here's when the problem arises. In the main TableView, instead of one option labelled "Add phone Number"
there are two.

I know that the singelton class which the TableView is using has been updated, it's the TableView that doesn't update in the correct manner. If I then switch between Edit/Done the tableView displays the information correctly.

I have tried to update the singleton class in the ViewDidLoad & ViewWillAppear methods but the result is the same, the first time that the view is reloaded it doesn't display the new row properly.

I have thought about overriding the "Back" BarButton to try and get the TableView to display correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2024-10-03 07:23:14

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

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

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

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

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

I didn't understand quite why duplicate rows had to do with updating the tableview. But it sounds a bit like you're creating two tables, somehow? Or even two viewcontrollers on top of each other, but I'll assume your navigation code is set up properly (using tabbars can cause this with incorrect viewcontroller initialization code).

You'd definitely want anything that changes the tables contents in ViewWillAppear.

Do you get the duplicate cells even when updating in ViewDidLoad (normally only called the first time the view appears). To execute when navigating back,

If you are looking to refresh the table data, use [tableview reloadData] in ViewWillAppear.

If all else fails, to trace this bug, I recommend to Add Expression on the tableView object and watch it change (in Debug mode).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文