可以通过在 中将高度设置为零来隐藏单元格- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
。
第一个选项是设置一些标志来隐藏空单元格。
在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if ([creator isEqual: userID]) {
[cell.contentView addSubview:imageView];
cell.textLabel.text = [[NSString alloc] initWithFormat:@" %@",[tempDictionary objectForKey:@"team_name"]];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@ %@ \n%@", [tempDictionary objectForKey:@"date"], [tempDictionary objectForKey:@"time"], [tempDictionary objectForKey:@"location"]];
hideCells = NO;
} else {
[imageView removeFromSuperview];
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
hideCells = YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == someCell && hideCells == YES)
return 0;
else
return 44;
}
第二种选择是在数据源字典中添加额外的键值对来跟踪是否需要隐藏单元格。
if ([creator isEqual: userID]) {
[cell.contentView addSubview:imageView];
cell.textLabel.text = [[NSString alloc] initWithFormat:@" %@",[tempDictionary objectForKey:@"team_name"]];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@ %@ \n%@", [tempDictionary objectForKey:@"date"], [tempDictionary objectForKey:@"time"], [tempDictionary objectForKey:@"location"]];
[array replaceObjectAtIndex:indexPath.row withObject:@"YES"];
} else {
[imageView removeFromSuperview];
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
[array replaceObjectAtIndex:indexPath.row withObject:@"YES"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[array objectAtIndex:indexPath.row]isEqualToString:@"NO"] )
return 0;
else
return 44;
}