当没有数据从 Parse 返回时,我试图从 TableView 中删除表中的一个单元格。但无论我尝试什么,我都找不到任何解决方案来避免在包含内容的单元格之前出现空白单元格(未传递数据时的单元格)。
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
static NSString *simpleTableIdentifier = @"RecipeCell";
NSString *identifier;
if (indexPath.row == 0) {
identifier = @"Cell";
} else if (indexPath.row == 1) {
identifier = @"Cell2";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// Configure the cell
if (![@"test" isEqual: [object objectForKey:@"teacher"]]){
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = @"";
UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
prepTimeLabel.text = @"";
return cell;
} else {
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = [object objectForKey:@"message"];
UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
prepTimeLabel.text = [object objectForKey:@"teacher"];
return cell;
}
}
感谢您提供的任何帮助!
最佳答案
您无法通过 cellForRowAtIndexPath
方法控制 UITableView
中的单元格数量 - 您需要从 numberOfRowsInSection
方法返回正确的值.引用UITableViewDataSource protocol reference和 Table View Programming Guide
此外,此代码块 -
if (indexPath.row == 0) {
identifier = @"Cell";
} else if (indexPath.row == 1) {
identifier = @"Cell2";
}
有点多余 - 您的所有单元格都是 UITableViewCellStyleDefault
,因此它们可以互换。没有必要使用两个不同的标识符 - 如果您打算在将来使用不同的自定义单元格类型,那么您可以保留这个概念,但您可能不会直接使用 indexPath.row,而是由数据类型驱动相关行的模型。
关于ios - UITableView cellForRowAtIndexPath : How to return no cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22544447/