UITableView动作行 [英] UITableView action row

查看:97
本文介绍了UITableView动作行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WWDC 11视频'会话125 - UITableCiew更改,提示,技巧'24:30 + Luke Hiesterman先生正在进行演示,当选择一个单元格时,会在表格视图中添加一个单元格。

On the WWDC 11 video 'session 125 - UITableCiew Changes, Tips, Tricks' at 24:30+ Mr Luke Hiesterman is giving a demo which adds a cell to the table view when a cell is selected.

我想将这个功能添加到我的IOS应用程序中,但我无法弄清楚如何制作它。

I want to add that functionality to my IOS application, but I can't figure out how to make it.

某些代码未在演示中显示 - 视频。并且没有可下载的演示来源。

Some code is not shown in the demo-video. And there is no downloadable source of the demo.

任何人都可以帮助我吗?

Can anyone help me out?

编辑:

我可以在所选行下面添加一个新行,但它是另一个自定义单元格。

I can add a new row below the selected row but it is an other custom cell.

(我有一个你可以接受的合约清单)

(I have a list of contracts which you can accept)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ((indexPath.row >= [_contracts count]) ? 40 : 60);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_contracts count] + ((_addedRow) ? 1 : 0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseID = @"contract_cell";

    ContractTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];

    if(!cell)
    {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ContractTableViewCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
        [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"contract_cell.png"]]];
    }

    NSLog(@"row?: %d", indexPath.row);
    //I thought this would work... not.
    if((indexPath.row >= [_contracts count]))
    {
        [cell.label setText:@"new"];
    }
    else
    {
        Contract *contract = [_contracts objectAtIndex:indexPath.row];

        [cell.label setText:@"test"];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!_addedRow)
    {
        _addedRow = YES;

        [tableView deselectRowAtIndexPath:indexPath animated:NO];

        [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
}

例如我按第一行。在第一行下面添加一行。但是现在为第2行调用了cellForRowAtIndexPath ..(需要是新的自定义)

For example I press the first row. A row is added below the first one. But now the cellForRowAtIndexPath.. is called for row 2 (needs to be the new custom)

如何检查它是否是新的?

how can I check if it is a new one?

推荐答案

您需要记住选择了哪些行以在正确的索引处显示添加的行。目前,您总是假设添加的行是最后一行,因为只有当索引大于您的合同数量时才在 tableView:cellForRowAtIndexPath:中进行配置。

You need to remember which of your rows is selected to show the added row at the correct index. Currently you're always assuming that the added row is the very last one because you're configuring it in tableView:cellForRowAtIndexPath: only if the index is bigger than your number of contracts.

假设您有五份合约,而第三份合同已被选中。 if((indexPath.row> = [_contracts count]))仅对最后一行为真,但实际上希望第四行的条件为真,所以它应该是 if(indexPath.row == selectedRowIndex + 1)(你需要在一些实例变量中存储选定的行索引)。

Say you have five contracts and the third one is selected. if((indexPath.row >= [_contracts count])) is only true for the last row but actually want this condition to be true for the fourth row, so it should be if (indexPath.row == selectedRowIndex + 1) (you need to store the selected row index in some instance variable).

这篇关于UITableView动作行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆