我正在尝试以手风琴单元格模式展开/折叠 tableview 单元格,这样,当用户点击行时,他们将通过扩展行高来获得单元格内所选行的详细描述。我有 2 个数组,'array' 和 'detailarray' 分别用于在 'cell.textLabel.text' 和 'cell.detailTextLabel.text' 单元格中显示它。现在最初我已将“cell.detailTextLabel.hidden = YES”设为隐藏,这样当用户点击该行时可以获得单元格的扩展文本。
我的代码,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text=[array objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
cell.detailTextLabel.hidden = YES;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"indexpath is %d", indexPath.row);
selectedIndex = indexPath.row;
isSearching = YES;
[self.tableview beginUpdates];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[array objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
cell.detailTextLabel.hidden = NO;
[self.tableview endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex)
{
return 77;
}
return 44;
}
现在我创建了一个有 4 行的 tableviewcell,当点击每一行时,我会得到所选行的扩展视图。在运行时,我可以在单元格上看到“detailarray”文本,当它扩展时,它会在 didselectRowAtIndexPath 函数停止时消失,为什么会这样,任何人都可以在这方面帮助我。