ios - 添加带有自定义文本的新 UITableView 行

标签 ios objective-c uitableview

使用这段代码

- (IBAction)testAdd:(id)sender 
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.numberOfRows inSection:0];

    [self.tableView beginUpdates];
    self.numberOfRows++;
    [self.tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView endUpdates];


}

我可以通过应用程序上的“添加”按钮向 tableView 添加新项目。这基本上添加了一个与它前面的表中已经存在的项目相同的项目。 例如,我有一个 TableView ,第一行显示字符串“TEST”,点击添加会添加另一行显示“TEST”。 我希望能够为新行传递一个自定义值,所以点击添加输出一行说“NEWTHING”。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];

    // Configure the cell...

    cell.textLabel.text = self.val2;

    return cell;
}

我的数据源实际上是另一个 View Controller ,它接受用户输入并将其发送到我的 tabelViewController,项目的文本为“val2”。 我真正想要实现的是点击添加,返回到用户输入 View Controller ,获取新数据并将其发送回我的 tableViewController 以进行显示的能力

最佳答案

您要问的是在 -cellForRowAtIndexPath: 中完成的一些事情(大多数时候,这取决于您设计数据源的方式) 但如果这对你来说无关紧要,那么你可以这样做:

- (IBAction)testAdd:(id)sender 
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.numberOfRows
                                                inSection:0];
    self.numberOfRows++;

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[indexPath]
                          withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView endUpdates];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [cell.textLabel setText:@"NEWTHING"];
}

但请注意,当您向上/向下滚动并返回到此单元格时,它很可能会显示“TEST”(这就是 -cellForRowAtIndexPath:将显示它的真正目的)

PS:如果您想继续,请在问题中包含您的 -cellForRowAtIndexPath: 方法实现


编辑:

您的 -cellForRowAtIndexPath 太静态了...从某种意义上说,它只是将 self.val2 设置为 cell.textLabel
假设您从 10 行开始,-cellForRowAtIndexPath 将被调用 10 次,每次都会将 self.val2 设置到当前单元格的 textLabel.
现在...当您添加一行时(点击按钮),将为第 11 个单元格调用 -cellForRowAtIndexPath 和相同的* 文本将被设置为它。
*这在技术上发生了,但我们很快更改了单元格的文本

基本上,tableView 不知道如何区分现有单元格和新添加的单元格,因为数据源本身不是动态的。

为了指导 tableView 如何处理不同的单元格,我们需要创建一个更动态的数据源。

有不同的使用方法,但我通常会这样做:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.val2 = @"TEST";

    //declare "NSMutableArray *arrDatasource;" globally
    //this will be the soul of the tableView
    arrDatasource = [[NSMutableArray alloc] init];

    int i_numberOfCells = 10;

    //populate beginning cells with default text
    for (int i = 0; i < i_numberOfCells; i++) {
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
        [dictionary setObject:self.val2 forKey:@"displayText"];

        [arrDatasource addObject:dictionary];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return number of objects in arrDatasource
    return arrDatasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];

    //pick up value for key "displayText" and set it onto the cell's label
    [cell.textLabel setText:arrDatasource[indexPath.row][@"displayText"]];
    //this will be dynamic in nature because you can modify the contents
    //of arrDatasource and simply tell tableView to update appropriately

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //make indexPath of new cell to be created
    NSIndexPath *indexPathNEXT = [NSIndexPath indexPathForItem:arrDatasource.count inSection:0];

    //add the appropriate contents to a dictionary
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:@"NEWTHING" forKey:@"displayText"];

    //add the dictionary object to the main array which is the datasource
    [arrDatasource addObject:dictionary];

    //add it to tableView
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[indexPathNEXT]
                          withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    //this ends up calling -cellForRowAtIndexPath for the newly created cell
    //-cellForRowAtIndexPath shows the text (you put in the dictionary in this method above)
}

PS: -cellForRowAtIndexPath: 每当单元格更新或刷新或需要显示时调用,因此需要正确实现此方法

关于ios - 添加带有自定义文本的新 UITableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24021457/

相关文章:

objective-c - @synchronized(self) 是否创建一个 block ,其中 self 前缀在属性上是不必要的?

swift - 如何快速更改表格 View 中文本的颜色?

ios - UIRefreshControl 卡在返回前台 iOS 10

ios - 旋转 UIView 导致它改变位置

php - gif 文件从 php 到 iphone

ios - cellForRow At IndexPath 执行一次太少?

ios - 访问 UITableViewController 单元格集合

ios - 将图像上传到 AWS 压缩时间过长?

iOS 13.2 MKTileOverlay 偶尔不会渲染

objective-c - 如何链接 Interface Builder View