Bar*_*obs 22 iphone cocoa-touch uitableview
如果您有一行普通(未分组)的UITableView,则屏幕的其余部分将填充空白或空单元格.你如何改变这些空白细胞的外观?首先,我认为他们会看到表格视图中使用的单元格,但似乎他们没有.
来自Cultured Code(Things)的人们在修改这些细胞方面做得很好,但我无法立即想出改变其外观的方法.
有小费吗?
基于samvermette的答案,但修改为直接使用背景图像而不是合成来自UITableViewCell子类的图像.Sam的答案很好,但它的效果不如直接创建背景图像.
创建一个UIView子类 - 在本例中我称之为CustomTiledView - 并将以下代码添加到类中:
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"tableview_empty_cell_image"];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM (context, 1, -1);
CGContextDrawTiledImage(context,
CGRectMake(0, 0, rect.size.width, image.size.height),
[image CGImage]);
}
Run Code Online (Sandbox Code Playgroud)
将以下代码添加到tableviewcontroller:
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section {
// this can be any size, but it should be 1) multiple of the
// background image height so the last empty cell drawn
// is not cut off, 2) tall enough that footer cells
// cover the entire tableview height when the tableview
// is empty, 3) tall enough that pulling up on an empty
// tableview does not reveal the background.
return BACKGROUND_IMAGE_HEIGHT * 9; // create 9 empty cells
}
- (UIView *)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section {
CustomTiledView *footerView = [[CustomTiledView alloc] init];
return [footerView autorelease];
}
Run Code Online (Sandbox Code Playgroud)
最后,您需要将tableview的底部内容插入设置为从此-tableView:heightForFooterInsection:示例返回的值的负数.在此示例中它将是-1*BACKGROUND_IMAGE_HEIGHT*9.您可以通过Interface Builder的Size Inspector或通过self.tableView.contentInsettableviewcontroller 设置属性来设置底部内容.
干杯!
我设置了一个~300px高的UIView子类到我的tableView页眉和页脚视图,调整tableView插件以便它们补偿这些视图(将顶部和底部插入设置为-300px).
我的UIView子类实现了drawRect方法,我在其中重复CGContextDrawTiledImage()绘制一个空UITableViewCell:
- (void)drawRect:(CGRect)rect {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 46),NO,0.0);
emptyCell = [[SWTableViewCell alloc] initWithFrame:CGRectZero];
[emptyCell drawRect:CGRectMake(0, 0, 300, 46)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
[emptyCell release];
UIGraphicsEndImageContext();
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM (context, 1, -1); // this prevents the image from getting drawn upside-down
CGContextDrawTiledImage(context, CGRectMake(0, 0, 300, 46), [newImage CGImage]);
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我的tableviewcells是46px高,所以如果我想让UIView子类包含8个这样的空单元格,我需要使它高368px.