xcode 实时:向 UITableViewController 中的表添加行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6143423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:08:06  来源:igfitidea点击:

RealTime: Adding rows to the table in UITableViewController

iphoneobjective-cxcodeuitableviewuibutton

提问by Legolas

I just checked the datasource and the UITableView of the app and they seem to be working well. I can add a row to the TableView by using the code - [...initWithObjects:(id),nil] and I could do this at code level.

我刚刚检查了应用程序的数据源和 UITableView,它们似乎运行良好。我可以使用代码 - [...initWithObjects:(id),nil] 向 TableView 添加一行,并且我可以在代码级别执行此操作。

This is what I want to do.

这就是我想要做的。

  1. Create an Add button or a '+' sign on top of the table.
  2. So while the App is running.. If I click that, I should be able to set a name for that row.
  3. If I need to create more rows and set names for them, I just press the add button and I again type new names for the rows .
  1. 在表格顶部创建一个添加按钮或一个“+”号。
  2. 所以当应用程序运行时.. 如果我点击它,我应该能够为该行设置一个名称。
  3. 如果我需要创建更多行并为它们设置名称,我只需按添加按钮,然后再次为行键入新名称。

How do I go about this in real time?

我如何实时处理这个问题?

回答by Seamus Campbell

The UITableView method -insertRowsAtIndexPaths:withRowAnimation:is used to add rows to a table programmatically.

UITableView 方法-insertRowsAtIndexPaths:withRowAnimation:用于以编程方式向表中添加行。

This answeraddresses your problem fairly directly.

这个答案相当直接地解决了您的问题。

回答by Cameron

It's fairly straightforward... and this is from memory, so I might have a typo here or there. The idea should work though.

它相当简单……这是凭记忆,所以我可能在这里或那里有一个错字。不过这个想法应该可行。

In tableView:numberOfRowsInSection:you give return [myArray count] + 1; (you're adding one row to the total)

tableView:numberOfRowsInSection:你给 return [myArray count] + 1; (您在总数中增加一行)

In tableView:cellForRowAtIndexPath:the cell where [indexPath row] == [myArray count] is where make a cell with "Add Row" text, rather than whatever your data source is, because it goes to [myArray count]-1. (I think that makes sense). for example,

tableView:cellForRowAtIndexPath:[indexPath row] == [myArray count] 所在的单元格中,使用“添加行”文本创建一个单元格,而不是您的数据源是什么,因为它转到 [myArray count]-1。(我认为这是有道理的)。例如,

    if([indexPath row] == [myArray count]){
        cell.textLabel.text = @"Add New Item";
    } else {
         cell.textLabel.text = [[myArray objectAtIndex:[indexPath row]]
                                           valueForKey:@"title"];            
    }
    return cell;

Then you would insert the new record into your myArray and the table.

然后将新记录插入到 myArray 和表中。

As for actually changing the new row details, you have some options of creating a custom cell with some kind of textual input or using a modal view controller with a prompt. It's really up to you.

至于实际更改新行的详细信息,您可以选择使用某种文本输入创建自定义单元格或使用带提示的模式视图控制器。这真的取决于你。