在 iOS7 中,分组 TableView 的单元格显示为 TableView 的全宽,更像是普通 TableView 样式。但在模拟器的设置应用程序中,分组样式看起来不同。对于实现这种类型的单元有什么帮助吗?
最佳答案
此解决方案适用于 iOS7 以及以前版本的 iOS。
创建自定义 UITableView 单元格类并覆盖 UITableViewCell。在 setFrame 中您可以调整所需的单元格大小。 setFrame 和 setAlpha 都是在 tableView:willDisplayCell:forRowAtIndexPath: 之后调用的,因此任何辅助 View 和 ImageView 都可以正确显示。
@interface CustomTableViewCell : UITableViewCell
@end
@implementation CustomTableViewCell
-(void) setFrame:(CGRect)frame
{
float inset = 20.0f;
frame.origin.x += inset;
frame.size.width -= 2 * inset;
[super setFrame:frame];
}
@end
在 cellForRowAtIndexPath 中,将 UITableViewCell 替换为 CustomTableViewCell。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell" forIndexPath:indexPath];
...
}
在 Storyboard文件中将原型(prototype)单元格类设置为CustomTableViewCell,并将其标识符设置为“CustomTableViewCell”
关于iOS7-UItableViewCell 以 Grouped 样式显示在表格 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18783270/