【问题标题】:Add new cell with expanded UITablviewcell添加具有扩展的 UITablviewcell 的新单元格
【发布时间】:2017-12-19 05:17:23
【问题描述】:

在我的应用程序中,我已经在其中完成了展开的表格视图单元格,我需要再次展开单元格。我面临再次插入展开的单元格的问题。有可能吗?

//cellForRowAtIndexPath:

 [cell.expandButton addTarget:self action:@selector(expandAction:) forControlEvents:UIControlEventTouchUpInside];

 if (indexPath.item < [array count] + 1) {
        NSLog(@"Show one cell”);

 }else{

        NSLog(@"Show another cell")

 }

//Button action
-(void) expandAction:(UIButton*)sender{
    if (indexPath.item < [array count] + 1) {
        NSLog(@"Show one cell”);
    }else{

        NSLog(@"Show another cell")

    }

}

【问题讨论】:

  • 没有代码如何提供帮助
  • 用 RATreeview 试试
  • @Anbu.Karthik 我需要在没有任何库的情况下实现。

标签: ios objective-c xcode uitableview expand


【解决方案1】:

如何生成 tableView 数据?如果你使用对象数组,那么你可以让它像这样:

CellItem 类保存单元格状态

@interface CellItem : NSObject
@property BOOL isExpanded;
@end

您的自定义单元格类。设置方法根据 CellItem 对象状态自定义您的单元格

@interface ExpandableCell : UITableViewCell
- (void)setupWith:(CellItem *)cellItem;
@end

@implementation ExpandableCell
- (void)setupWith:(CellItem *)cellItem {

    if(cellItem.isExpanded) {
        //Expand your cell
    } else {
        //Don't exp and
    }
}
@end

获取 tableView 数据的对象数组

@property NSArray<CellItem *> *items;

协议,它定义了单元状态委托的方法。在类中实现此方法,该类也是 yourtableView 的委托。每次用户展开/关闭它时,从单元格调用此方法。在实现中将 cellItem 保存在项目数组中。

@protocol ExpandableCellDelegate
- (void)cellDidChangeStateWithCellItem: (CellItem *)cellItem;
@end

UITableView 单元格是可重复使用的,因此每次单元格出现在屏幕上时,您都应该重新自定义它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellItem *cellItem = self.items[indexPath.row];
    ExpandableCell *cell = (ExpandableCell *)[tableView dequeueReusableCellWithIdentifier:@"InsertYourCEllID"];
    [cell setupWith:cellItem];
    return cell;
}

【讨论】:

    猜你喜欢
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2018-07-27
    • 2015-12-11
    相关资源
    最近更新 更多