【问题标题】:iPhone + UITableViewiPhone + UITableView
【发布时间】:2009-10-22 05:23:32
【问题描述】:

我的应用程序中有 UITableView,我想对其进行格式化,例如更改表格中的行高、更改单元格中文本的字体和颜色等。

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    听起来您应该阅读 Apple 的 Table View Programming Guide for iOS 中的 A Closer Look at Table-View Cells

    更改文本颜色和字体

    如果您使用的是标准表格视图单元格,您可以在tableView:cellForRowAtIndexPath: 数据源方法中自定义其textLabel(或detailLabel)标签的文本颜色和字体:

    - (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;
    }
    

    更改行高

    如果每一行都将是相同的高度,您应该设置UITableViewrowHeight 属性:

    [tableView setRowHeight:42];
    

    如果行将具有可变高度,那么您可以使用UITableViewtableView:heightForRowAtIndexPath: 委托方法:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        CGFloat height = 42;
        if ([indexPath row] == 4) {
            height = 21;
        }
        return height;
    }
    

    如果您想彻底改变表格视图单元格的外观,您可能需要查看 @mfu 建议的 Matt Gallagher 的 Easy custom UITableView drawing 文章。但是,如果您开始走到这一步,请确保您确切知道自己在做什么——大多数时候您会希望坚持使用 Apple 的默认样式。

    【讨论】:

      【解决方案2】:

      你的问题比较笼统。 你可以在这里参考非常好的文章来获得一些关于如何编写自定义表格视图的基本想法'easy custom uitableview drawing'

      【讨论】:

        【解决方案3】:

        您应该查看UITableViewCell 的子类化,使用新的子类,您可以在单元格中拥有任何您想要的东西 - 其他视图、按钮、标签等。

        Apple 有很多很好的示例。有关示例列表,请参阅 this

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多