我需要動態更改單元格的行。這就是爲什麼我使用的selectedIndex變量來存儲選定行heightForRowAtIndexPath涉及很多單元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
TarModel * tar = self.tarList[indexPath.row];
if (selectedIndex == indexPath.row) {
static NSString *CellIdentifier = @"detailCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
else {
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
// Configure the cell...
cell.textLabel.text = tar.way;
cell.detailTextLabel.text = [NSString stringWithFormat:@"cost: € %@",
tar.price];
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 reloadData];
}
,然後我改變我的選擇行與此高度:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height;
if(selectedIndex == -1) {
height = 44.0;
}
else if (selectedIndex == indexPath.row) {
height = 88.0;
}
else {
height = 44.0;
}
return height;
}
當我運行此代碼,第一行
選擇一切是好的,但如果第二個(mytableview中只有兩行)被選中,我甚至會得到修改高度的所有下一個空行。我哪裏錯了?
heightForRowAtIndexPath不叫每個用戶選擇行時間。 –
@BijoyThangaraj OP在用戶選擇一行時重新加載UITableView – Tim
selectedIndex定義在哪裏?你不需要導致返回44的兩個條件,只有一個。 – Tim