题
我在我的应用程序中使用UITableView,我想对其进行格式化,例如更改表格中行的高度,更改单元格中文本的字体和颜色等。
解决方案
听起来你应该阅读仔细查看表格单元格 rel =“nofollow noreferrer”> iOS视图编程指南。
更改文字颜色和字体
如果您使用的是标准表格视图单元格,则可以在 textLabel (或 detailLabel
)标签的文本颜色和字体。 “https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614861-tableview?language=objc"rel =”nofollow noreferrer“> tableView:cellForRowAtIndexPath:
数据源方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/* Cell initialisation code... */
/* Configure Cell */
[[cell textLabel] setTextColor:[UIColor grayColor]];
[[cell textLabel] setFont:[UIFont fontWithName:@"Marker Felt" size:22]];
return cell;
}
更改行的高度
如果每一行的高度相同,则应设置 UITableView
的 rowHeight
属性:
[tableView setRowHeight:42];
如果行的高度可变,那么您可以使用 tableView:heightForRowAtIndexPath:
委托 UITableView
的方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 42;
if ([indexPath row] == 4) {
height = 21;
}
return height;
}
如果您想彻底改变表格视图单元格的外观,您可能需要查看Matt Gallagher的简单的自定义UITableView绘图文章,由@mfu建议。但是,如果你开始走这么远,请确保你确切知道自己在做什么。大部分时间你都会想要坚持Apple的默认风格。
其他提示
你的问题很普遍。 你可以在这里参考非常好的文章来了解如何编写自定义表视图的基本知识“轻松自定义uitableview绘图”
您应该查看 UITableViewCell 的子类化,并使用该新的子类,您可以拥有任何内容你想要在单元格内 - 其他视图,按钮,标签等
Apple有很多很好的例子。有关样本列表,请参见此。