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

时间:2014-06-03 17:31:41

标签: 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",点击add添加另一行显示" 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;
}

我的数据源实际上是另一个视图控制器,它接收用户输入并将其发送到我的tabelViewController,项目的文本为" val2"。 我实际想要实现的是能够点击添加,返回到用户输入视图控制器,获取新数据并将其发送回我的tableViewController以显示

1 个答案:

答案 0 :(得分:1)

你问的是,在-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:,因此需要正确实现此方法