2017-07-14 82 views
0

在我的应用程序中,我已经完成了扩展表格视图单元格,同样需要展开单元格。我正在面临再次插入扩展单元格的问题。是否可以这样做?使用扩展的UITablviewcell添加新单元格

//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") 

    } 

} 
+0

与出码如何能够帮助 –

+0

别的尝试RATreeview –

+0

@ Anbu.Karthik我需要没有任何库来实现。 –

回答

0

你如何生成你的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; 
}