Xcode 附件为UITableViewCellAccessoryCheckmark时如何将UITableViewCell中的文本居中
当accessoryView设置为UITableViewCellAccessoryCheckmark时,我试图将文本保持在UITableViewCell的中心位置。我也在这门课上使用故事板 这是我不想要的东西的截图 如何防止文本向左缩进 我的willDisplayCell方法Xcode 附件为UITableViewCellAccessoryCheckmark时如何将UITableViewCell中的文本居中,xcode,ios5,uitableview,Xcode,Ios5,Uitableview,当accessoryView设置为UITableViewCellAccessoryCheckmark时,我试图将文本保持在UITableViewCell的中心位置。我也在这门课上使用故事板 这是我不想要的东西的截图 如何防止文本向左缩进 我的willDisplayCell方法 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexP
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [self cellToCheckmark]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
NSLog(@"Not Being Checked");
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
if (cell.shouldIndentWhileEditing == YES) {
cell.shouldIndentWhileEditing = NO;
}
}
CellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Google";
break;
case 1:
cell.textLabel.text = @"Bing";
break;
case 2:
cell.textLabel.text = @"Yahoo!";
break;
default:
break;
}
}
if (indexPath.row == [self cellToCheckmark]) {
NSLog(@"Being Checked");
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
NSLog(@"Not Being Checked");
cell.accessoryType = UITableViewCellAccessoryNone;
}
if (cell.shouldIndentWhileEditing == YES) {
cell.shouldIndentWhileEditing = NO;
}
return cell;
}
您需要做两件事: 首先,您需要确保将表格样式设置为默认值:
UITableViewCellStyleDefault
。所有其他样式都以某种方式使用detailTextLabel
,您将无法设置textLabel的对齐属性
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
然后,您可以设置单元格文本标签的对齐方式:
cell.textLabel.textAlignment = NSTextAlignmentCenter;
然后根据您的数据要求将附件设置为选中标记
cell.accessoryType = ( myDataMatches ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone );
截图
不知道问题是否已解决
if (/* your condition */) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.indentationLevel = 4;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.indentationLevel = 0;
}
iOS 8+解决方案:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = ...;
if (/* your condition */) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.layoutMargins = UIEdgeInsetsMake(0, 40, 0, 10);
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10);
}
return cell;
}
这些解决方案适用于默认单元格。如果您有自己的自定义单元格,最简单的方法是使标签同时具有左边框和右边框的约束,然后为左边框约束创建一个出口,并在
cellforrowatinexpath
方法中设置其常量:
cell.accessoryType = isChosen ? .checkmark : .none
cell.leftMarginConstraint.constant = isChosen ? 40 : 0
这样,为accessoryType添加的右边距也会添加到左边。似乎有一个更简单的答案(请参阅)。简而言之,您正在单元格的内容视图中将标签居中。取而代之的是,把它放在细胞本身的中心,这样它就不会移动了 经过试用,它可以在iOS 11.2上运行
对不起,我忘了提到我在这门课上使用了故事板。这会如何改变答案呢?不管你是否使用故事板。这些属性仅适用于
UITableView
我已更新了我的问题。我使用静态单元格而不是原型有关系吗?如果你把代码放在cellforrowatinexpath:
中应该可以。这就是我在表格中居中文本的方式,我有一个使用复选标记的表格。在我的回答中看到我的屏幕截图。包括您的cellForRowAtIndexPath:
中的代码,您可能还有其他事情要做。是否有关于为什么是40的文档?@Sonicasas没有,只是经验观察。我没有找到任何苹果的常量或运行时值。