Cocoa touch UITableViewCell中的字幕对齐
我正在尝试构建一个单元格中包含字幕文本的表格视图 问题是,当我试图将字幕文本的对齐方式设置为右侧时,它不起作用,但与主文本配合良好 这是我的密码Cocoa touch UITableViewCell中的字幕对齐,cocoa-touch,uitableview,uilabel,Cocoa Touch,Uitableview,Uilabel,我正在尝试构建一个单元格中包含字幕文本的表格视图 问题是,当我试图将字幕文本的对齐方式设置为右侧时,它不起作用,但与主文本配合良好 这是我的密码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; UITableViewCe
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setTextAlignment:UITextAlignmentRight];
[[cell detailTextLabel] setTextAlignment:UITextAlignmentRight];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:18];
cell.detailTextLabel.text = @"test";
return cell;
}
当我删除字幕代码时,对齐效果很好
有什么想法吗?好的,将UITableView单元格子类化,并使用init自定义标签。可以替代布局子视图并将标签向右移动:
- (void)layoutSubviews {
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0.0, 68.0, 80.0, self.frame.size.height);
self.detailTextLabel.frame = CGRectMake(0.0, 68.0, 120.0, self.frame.size.height);
}
这些只是示例值,您可以了解这些值。为什么要两次初始化单元格:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
您可以创建带有两个标签的自定义单元格,并提供与这些标签的对齐方式。请执行以下步骤:
1.添加UITableViewCell的新文件子类,如labelCustomCell。
2.在labelCustomCell中创建两个标签,即label1和label2。
3.在initWithStyle方法中,分配这些标签并提供对齐。
4.在layoutSubViews方法中,将框架指定给这些标签。
5.在CellForRowatineXpath方法中编写以下代码:
static NSString *CellIdentifier = @"DataEntryCell";
labelCustomCell *cell = (labelCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[labelCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.label1.text = [array objectAtIndex:indexPath.row];
cell.label1.font = [UIFont systemFontOfSize:18];
cell.label2.text = @"test";
别忘了导入labelCustomCell。@H.alvarsi如果我能提供任何帮助,请告诉我:)