展开/折叠具有不同单元格和数据源的UITableViewCell [英] Expand / Collapse UITableViewCell with different cell and data source

查看:102
本文介绍了展开/折叠具有不同单元格和数据源的UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个充满plist数据源的UITableView,并用Interface Builder做了自定义单元(所以我使用的是xib文件)

I did a UITableView filled with a plist data source, and I did custom cells with Interface Builder (so I'm using xib files)

这是我在其中创建单元格的代码:

here's the part of code where I'm creating cells:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"DataTableViewCell" owner:nil options:nil];

        for (UIView *view in views) {
            if ([view isKindOfClass:[UITableViewCell class]]) {

                cell = (DataTableViewCell*)view;

            }
        }
    }

    return cell;
}

然后当我使用以下命令时:

then when I use the:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

方法来获取选定的单元格,我将此方法扩展为该单元格:

method to get the selected cell, I call this method to expand the cell:

- (void)expandCellAtIndex:(int)index {

    NSMutableArray *path = [NSMutableArray new];
    NSArray *currentCells = [subCellItems objectAtIndex:index];
    int insertPos = index + 1;
    for (int i = 0; i < [currentCells count]; i++) {
        [path addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
    }

    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

现在的问题是,因为我没有这样做之前,我在如何更改要扩展时要显示的单元格的逻辑上停留不动,这是因为方法:

Now the problem is that since I've not done this before, I'm stuck on the logic on how to change the cell I want to show when expanded, that's because the method:

[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

正在搜索正确的路径,并插入一个与我的单元格xib文件相同的单元格xib我已经在开始时加载了数据

is searching for the right path and inserting a cell with the same cell xib file as the one I've loaded with my data at the start

如何设置一个不同单元格(具有不同的xib和不同的数据)来显示

how can I set a different cell (with a different xib, and different data) to show instead of the same as before?

基本上我有这张桌子,但是在扩展单元格中,我看到了您触摸的单元格副本

basically I have this table working but in the expanding cell I see a copy of the cell you touch

推荐答案

您可以这样做:

在.h中:

@interface TSViewController : UIViewController
{
    NSMutableArray* subCellIndexPaths;
}

在DataTableViewSubCell.xib中创建一个表视图单元格(您拥有其他视图去除)。将自定义类的类更改为DataTableViewSubCell。重新播放第二个单元格。

In DataTableViewSubCell.xib create one "Table View Cell" (other Views you have to remove). Change Class for "Custom Class" to DataTableViewSubCell. Replay it for second cell.

在.m中:

- (UITableViewCell*) tableView: (UITableView*) tableView
         cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
    static NSString *CellIdentifier = @"Cell";

    DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        if ([subCellIndexPaths containsObject: indexPath])
        {
            NSArray* nib = [[NSBundle mainBundle] loadNibNamed: @"DataTableViewSuperCell"
                                                         owner: nil
                                                       options: nil];

            cell = [nib objectAtIndex: 0];
        }
        else
        {
            NSArray* nib = [[NSBundle mainBundle] loadNibNamed: @"DataTableViewSubCell"
                                                         owner: nil
                                                       options: nil];

            cell = [nib objectAtIndex: 0];
        }
    }

    return cell;
}

- (void) expandCellAtIndex: (int)index
{
    NSMutableArray* indexPaths = [NSMutableArray new];
    NSArray* currentCells = [subCellItems objectAtIndex: index];

    int insertPos = index + 1;

    for (int i = 0; i < [currentCells count]; i++)
    {
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow: insertPos++
                                                    inSection: 0];
        [indexPaths addObject: indexPath];
        [subCellIndexPaths addObject: indexPath];
    }

    [self.tableView insertRowsAtIndexPaths: indexPaths
                          withRowAnimation: UITableViewRowAnimationFade];

    [self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: index
                                                               inSection: 0]
                          atScrollPosition: UITableViewScrollPositionTop
                                  animated: YES];

}

当然,您必须在任何地方创建subCellIndexPaths(在init或viewDodLoad方法)。

And of course you must create subCellIndexPaths anywhere (in init or viewDodLoad methods).

这篇关于展开/折叠具有不同单元格和数据源的UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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