Dựa trên câu trả lời samvermette's, nhưng được sửa đổi để sử dụng hình nền trực tiếp thay vì tổng hợp hình ảnh từ lớp con UITableViewCell. Câu trả lời của Sam là tốt, nhưng nó sẽ kém hiệu quả hơn là chỉ tạo một hình nền trực tiếp.
Tạo một phân lớp UIView - trong ví dụ này, tôi gọi nó là CustomTiledView - và thêm đoạn mã sau vào trong lớp:
- (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]);
}
Thêm mã sau đây để tableviewcontroller của bạn:
- (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];
}
Cuối cùng, bạn sẽ cần phải đặt nội dung bên dưới của tableview thành giá trị âm của giá trị được trả về từ -tableView:heightForFooterInsection:
Trong ví dụ này, nó sẽ là -1*BACKGROUND_IMAGE_HEIGHT*9
. Bạn có thể đặt nội dung bên dưới hoặc từ Trình kiểm tra kích thước của Trình tạo giao diện hoặc bằng cách đặt thuộc tính self.tableView.contentInset
từ bảng điều khiển tableviewcontroller.
Chúc mừng!
* Bump * có một giải pháp mà làm việc? Hãy chia sẻ ở đây. Thanks – lostInTransit
Những gì tôi đã làm cho đến nay là thêm một ô 'vô hình' ở cuối chế độ xem bảng với một lần xem hình ảnh hiển thị bóng của phần cuối của chế độ xem bảng. Tôi đã đặt kiểu dấu phân cách thành không. Điều này tạo ra một hiệu ứng tương tự nhưng tôi sẽ phải tinh chỉnh nó nhiều hơn một chút để được hạnh phúc với nó. –