[英]Load a custom XIB for expanded cell in tableView iOS?
我正在嘗試為UITableViewCell
選中時加載一個擴展單元格XIB。我的常規單元格和擴展單元格的內容幾乎相同,除了我將在擴展單元格中填充一些視圖的額外空間之外。單元格和擴展單元格我已將擴展單元格內的UILabel
顏色更改為其他顏色。單擊單元格時我能夠加載擴展單元格XIB, 但是 問題是每個單元格都被擴展單元格XIB取代 。唯一的區別是是單擊的單元格的高度是否比其他單元格高。當我單擊擴展的單元格時,將再次加載普通單元格XIB
。如何僅為單擊的單元格加載擴展的單元格XIB
? 還是有更好的方法來實現這一目標?
這就是我擴展單元格的方式。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.expandedCells containsObject:indexPath]) {
[self.expandedCells removeAllObjects];
isExpanded = NO;
[self.busTableView beginUpdates];
[self.busTableView reloadData];
[self.busTableView endUpdates];
}else{
isExpanded=YES;
if (self.expandedCells.count>0) {
[self.expandedCells removeAllObjects];
}
[self.expandedCells addObject:indexPath];
expCell.expContainer.hidden=YES;
//Some more code
[self.busTableView beginUpdates];
[self.busTableView reloadData];
[self.busTableView endUpdates];
}
}
而且cellForRow
方法看起來像這樣。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (!isExpanded) { //BOOL variable set to NO in viewDidLoad initially
BusListCell *cell =(BusListCell*) [tableView dequeueReusableCellWithIdentifier:nil];
if (cell==nil) {
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"BusListCell" owner:self options:nil];
cell = nibs[0];
}
cell.busName.text = [[busArray objectAtIndex:indexPath.row]valueForKey:@"operatorName"];
return cell;
}else{
expCell = (ExpandedCell*)[tableView dequeueReusableCellWithIdentifier:nil];
if (expCell==nil) {
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil];
expCell = nibs[0];
}
expCell.bus_Name.text = [[busArray objectAtIndex:indexPath.row]valueForKey:@"operatorName"];
return expCell;
}
return nil;
}
而不是檢查cellForRowAtIndexPath:
if (!isExpanded) {}
你必須檢查
if (![self.expandedCells containsObject:indexPath]){}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.