Ios UITableViewController-单元格大小分隔符和背景色
我正在尝试为我的UITableViewController实现airbnb查找Ios UITableViewController-单元格大小分隔符和背景色,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试为我的UITableViewController实现airbnb查找 分隔符不在屏幕的全宽范围内 每个单元格都有唯一的大小和背景颜色 我已设法使用静态表处理#2,并在IB中设置每个单元格大小,但存在以下问题: 未在IB中设置背景色(更改了背景色和色调) 我希望以编程方式“删除”单元格,因为它没有内容,只有一个函数返回单元格高度-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(nsindepath*)
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(nsindepath*)indepath
对于所有单元格都是通用的,并被称为preor,以使用数据填充单元格,因此在这种方法中,我无法决定是否应删除单元格我认为应该使用表视图的委托方法
willDisplayCell:forrowatinexpath:
。在单元格准备好显示在表视图上之前调用此委托方法。在此委托中,您将创建单元格,如果您需要在此处进行一些修改,这些修改将在显示单元格之前反映出来
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor redColor]];
// and all other cell customizations
}
// 1. Make full width separators:
- (void)viewWillLayoutSubviews {
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
#pragma mark - Table view data source
// 2.Customize cells background color and height
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = [NSString stringWithFormat:@"s%ld-r%ld", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
if (indexPath.row % 2) {
cell.backgroundColor = [UIColor redColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.text = cellIdentifier;
} else {
cell.backgroundColor = [UIColor cyanColor];
cell.contentView.backgroundColor = [UIColor whiteColor];//background color of contentView overlaps cell's background color
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; //getting cell at indexPath
if ([self isCellEmpty:cell]) {
return 22;
}
return 44;
}
- (BOOL)isCellEmpty:(UITableViewCell *)cell {
return !cell.textLabel.text.length; //Place your code for checking cell's content
}
您无法从静态
UITableView
中删除单元格,因为您没有实现cellForRow…
。查看问题了解更多信息。感谢您编写了如此详细的解决方案。不幸的是,它仍然存在调用顺序的基本问题,我的表数据在加载视图后被填充,因此在函数“heightforrowatinexpath”时,“willDisplayCell”等被称为my data is nil,无法决定单元格是否被“删除”。您可以添加数据状态检查:在这些方法的开头添加if(!data){return;},并在数据初始化后立即调用[tableView reloadData]。