【问题标题】:How to re-use some cells from UITableVIewController storyboarding mode Static Cells如何重用 UITableVIewController 故事板模式中的某些单元格静态单元格
【发布时间】:2012-03-16 18:47:02
【问题描述】:

我有这个UITableViewController,我使用Interface Builder 制作了一个Static Cells,并添加了三个不同类型的单元格。

第一个单元格是一些细节,最后一个单元格允许用户输入,然后显示中心单元格

我想重用中心单元格并在用户在最后一个评论框中输入时添加新的单元格。

我的问题是,因为它是静态单元格,我认为我不能单独重用中心单元格。我该如何解决?

【问题讨论】:

    标签: xcode ios5 xcode4.2 uitableview storyboard


    【解决方案1】:

    不要使用静态单元格。静态单元格不能重复使用。使用静态单元格,您可以仅在 xib 中创建一个表格。

    创建三个不同的原型单元,并给它们一个不同的重用标识符,并像常规单元一样使用它们。

    由于您的表格被分成多个部分,因此只需使用 indexPath 的部分信息来返回正确的单元格。

    这样的事情应该可以工作:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 3;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (section == 1) {
            return _objects.count;
        }
        return 1;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = nil;
        if (indexPath.section == 1) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"SecondCell"];
            // configure the mid cells
            NSDate *object = [_objects objectAtIndex:indexPath.row];
            cell.textLabel.text = [object description];
        }
        if (indexPath.section == 0) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"FirstCell"];
            // configure first cell
        }
        else if (indexPath.section == 2) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"ThirdCell"];
            // configure last cell
        }
        return cell;
    }
    

    也请查看tableFooterViewtableHeaderView 属性。也许您根本不需要单元格作为第一个和最后一个项目。

    【讨论】:

    • 谢谢你,我测试过并且工作起来就像一个魅力,我缺乏在原型中使用多个标识符的知识。
    猜你喜欢
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多