OGeek|极客世界-中国程序员成长平台

标题: ios - 将 UIView 添加到重用的 UITableViewCell? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:08
标题: ios - 将 UIView 添加到重用的 UITableViewCell?

我拼命想达到以下效果:我有一个表格 View ,我想在其中扩展选择的单元格并显示一些附加信息。我重复使用表格单元格,仅供引用。在过去,我能够在另一个应用程序中实现这样的效果,但这似乎对我根本不起作用。所有对发生这种情况至关重要的方法都被调用,并且行扩展得很好,但是 showExpandedContents 方法中的 additionalInfo 表不会出现!

所以这里有一些代码给你:

这是单元格设置:

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath{

    static NSString *cellID = @"MyCustomCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if(cell == nil){
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    ((MyCustomCell*)cell).data = [arrayToDisplay objectAtIndex:indexPath.row];


    return cell;
}

这里是选择方法:

-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath{


    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    //setting the current cell as the expandedRow, so that I can collapse it later
    expandedRow = [tableView cellForRowAtIndexPath:indexPath];
    [((MyCustomCell*)expandedRow) showExpandedContents];
    [tableView beginUpdates];
    [self changeRowHeightAtIndex:indexPath.row height:330];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [tableView endUpdates];



}

这是 MyCustomCell 中的 -showExpandedContents 方法,我想在其中显示附加信息:

-(void)showExpandedContents{

    additionalInfo = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, self.frame.size.width, 250)];
    [additionalInfo loadContent];
    additionalInfo.backgroundColor = UIColorFromRGB(0xf9f9f9, 1.0);
    additionalInfo.layer.zPosition = MAXFLOAT;
    [self.contentView addSubview:additionalInfo];

}



Best Answer-推荐答案


看起来像你的

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

正在为您提供一个新单元格,与调用 showExpandedContents 的单元格不同。

删除此行应该可以解决问题。

关于ios - 将 UIView 添加到重用的 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19735893/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4