ios - 将自定义 UITableViewCell 内容与标题对齐

标签 ios uitableview

我正在创建一个带有静态单元格的 TableView 。一些单元格需要自定义样式,因为它们包含左侧的标签和右侧的文本字段。

我想像 Basic 单元格样式那样将左侧的标签与节的标题对齐,但我找不到执行此操作的方法。

这是我的应用目前的样子:

My App

这是基本样式中对齐标签的样子:

System Preferences

我正在使用带有大小类和约束的自动布局。

最佳答案

我也遇到过类似的问题。

我的要求是,

1) 创建一个有标题和单元格的表格 View 。

2) 标题将有 3 个标签,在这些标签下,单元格将保存标题中各个标签的相应数据。

为了达到上述要求,我在标题中构建了我的标签,并根据标题中标签的位置和大小,根据标题标签重新构建了单元格的标签。

我在 TableView 的委托(delegate)方法中实现了上述内容 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

并根据标题在其中的位置和大小重新构建每个单元格的标签。

例如-

-(void) tableView:(UITableView *) tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
         // customize the cell just before it will be displayed

         // access section header
         UIView *sectionHeader = [tableView headerViewForSection:indexPath.section];

         // access section header label
         UILabel *headerLeftLabel = (UILabel *)[sectionHeader viewWithTag:HEADER_LABEL_TAG];

         // access cell's corresponding label which is to be aligned as the header's above label 
         UILabel *leftCellLabel = (UILabel *)[cell.contentView viewWithTag:CELL_LABEL_TAG];

         // align/ reframe the cells label with the corresponding label in the header
         [leftCellLabel setFrame: headerLeftLabel.frame];

         // similar for other labels

}

关于ios - 将自定义 UITableViewCell 内容与标题对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29408917/

相关文章:

iphone - 从 Nib 问题加载自定义单元格

java - setOnEditCommit JavaFX 后禁用 TableCell

ios - UITableViewCell 掩码选择区域/选择区域的大小?

ios - 编辑时tableView中没有didSelectRowAtIndexPath()

ios - IOS7 中的 AVCaptureSession 捕获输出崩溃(AVCaptureVideoDataOutput)

ios - Swift 自动创建临时字典

ios - 如何以视觉语言格式编写内在大小的内容拥抱优先级?

ios - UITableViewCell 复选标记在点击时打开和关闭

ios - 自定义 UITableViewCell : SIGABRT on "loadNibNamed:"

android - 在 Android 和 iOS 上实现 React Native 推送通知的最可靠方法是什么