我有一个使用手风琴视图的UITableView。并且我喜欢将父单元格分成带有标题的2个部分但不太确定如何去做这样做,因为我之前从未使用过手风琴视图而IB被删除,因为它主要是基于代码的。代码如下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return[topItems count] + ((currentExpandedIndex > -1) ? [[subItems objectAtIndex:currentExpandedIndex] count] : 0);
}
- (ACNBookingCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Define cell identifiers
static NSString *ParentCellIdentifier = @"ParentCell";
static NSString *ChildCellIdentifier = @"ChildCell";
// Check to see if cell isChild
BOOL isChild = currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];
// Initialise cell
ACNBookingCell *cell;
if (isChild && currentExpandedIndex == 0) {
}
else if (isChild) {
cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
}
if (cell == nil) {
cell = [[ACNBookingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ParentCellIdentifier];
}
// Current Orders cell
if (isChild && currentExpandedIndex == 0) {
cell.textLabel.text = @"Child Cell";
}
// History cell
else if (isChild && currentExpandedIndex == 2) {
}
// Favorites cell
else if (isChild) {
cell.textLabel.text = @"Child Cell";
}
// Parent cell
else {
int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex) ? (int)indexPath.row - (int)[[subItems objectAtIndex:currentExpandedIndex] count] : (int)indexPath.row;
cell.cellTitle.text = [topItems objectAtIndex:topIndex];
NSLog(@"%ld",(long)indexPath.row);
switch (indexPath.row) {
case 0:
cell.icon.image = [UIImage imageNamed:@"calendar"];
break;
case 1:
cell.icon.image = [UIImage imageNamed:@"globe"];
break;
case 2:
cell.icon.image = [UIImage imageNamed:@"taxi"];
break;
case 3:
cell.icon.image = [UIImage imageNamed:@"hotel"];
break;
case 4:
cell.icon.image = [UIImage imageNamed:@"calendar"];
break;
case 5:
cell.icon.image = [UIImage imageNamed:@"taxi"];
break;
}
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
ACNBookingCell *cell = (ACNBookingCell *)[self.tableView cellForRowAtIndexPath:indexPath];
// Check to see if cell isChild
BOOL isChild = currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];
// Action behind Child cell
if (isChild) {
if (currentExpandedIndex == 0) {
childIndexPath = indexPath;
}
else if (currentExpandedIndex == 2) {
childIndexPath = indexPath;
}
return;
}
[self.tableView beginUpdates];
// Expand parent cell to show child cells
if (currentExpandedIndex == indexPath.row) {
cell.arrowImage.image = [UIImage imageNamed:@"collapse"];
[self collapseSubItemsAtIndex:currentExpandedIndex];
currentExpandedIndex = -1;
}
// Collapse parent cell and hide child cells
else {
cell.arrowImage.image = [UIImage imageNamed:@"drop_down"];
BOOL shouldCollapse = currentExpandedIndex > -1;
if (shouldCollapse) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
}
currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? (int)indexPath.row - (int)[[subItems objectAtIndex:currentExpandedIndex] count] : (int)indexPath.row;
[self expandItemAtIndex:currentExpandedIndex];
}
[self.tableView endUpdates];
}
答案 0 :(得分:0)
您必须将numberOfSections设置为2或任何您想要的内容。然后你可以在章节中更改标题和死亡单元格。