xcode 当附件为 UITableViewCellAccessoryCheckmark 时,如何在 UITableViewCell 中居中文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10841693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to center text in a UITableViewCell when accessory is UITableViewCellAccessoryCheckmark
提问by W Dyson
I'm trying to keep text centred in a UITableViewCell when I have the accessoryView set to UITableViewCellAccessoryCheckmark. I'm also using a storyboard with this class.
当我将附件视图设置为 UITableViewCellAccessoryCheckmark 时,我试图使文本在 UITableViewCell 中居中。我也在这个课程中使用故事板。
Here's a screenshot of what I don't want.
这是我不想要的截图。
How do I stop the text from being indented to the left?
如何阻止文本向左缩进?
My willDisplayCell method..
我的 willDisplayCell 方法..
- (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:
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;
}
回答by WrightsCS
There are 2 things you need to do:
您需要做两件事:
First, you need to make sure you set the table style to Default: UITableViewCellStyleDefault
. All other styles use a detailTextLabel
in one way or another and you won't be able to set the textLabel's alignment property.
首先,您需要确保将表格样式设置为 Default: UITableViewCellStyleDefault
。所有其他样式都detailTextLabel
以一种或另一种方式使用 a并且您将无法设置 textLabel 的对齐属性。
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
Then you can set the alignment of your cell's textLabel:
然后您可以设置单元格 textLabel 的对齐方式:
cell.textLabel.textAlignment = NSTextAlignmentCenter;
Then setting the accessory to checkmark based on whatever your data requires.
然后根据您的数据需要将配件设置为复选标记。
cell.accessoryType = ( myDataMatches ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone );
Screenshot
截屏
回答by Gleb Tarasov
iOS 8+ solution:
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;
}
回答by Epaga
These solutions work for default cells. If you have your own custom cell, the easiest way to do it is make the label have constraints both to the left margin and the right margin, then create an outlet for your left margin constraint, and set its constant in the cellForRowAtIndexPath
method:
这些解决方案适用于默认单元格。如果您有自己的自定义单元格,最简单的方法是使标签对左边距和右边距都有约束,然后为左边距约束创建一个出口,并在cellForRowAtIndexPath
方法中设置其常量:
cell.accessoryType = isChosen ? .checkmark : .none
cell.leftMarginConstraint.constant = isChosen ? 40 : 0
This way, the right margin which is added for your accessoryType is then added to the left as well.
这样,为您的附件类型添加的右边距也会添加到左侧。
回答by Daniel
There seems to be a much simpler answer (See this answer). In short, you are centering the label in the cell's content view. Instead, center it in the cell itself, then it won't move.
似乎有一个更简单的答案(请参阅此答案)。简而言之,您在单元格的内容视图中将标签居中。相反,将它放在单元格本身的中心,然后它就不会移动。
Tried it out, it works on iOS 11.2.
试过了,它适用于 iOS 11.2。
回答by Jeremy Piednoel
Don't know if the problem is solve
不知道问题是否解决
if (/* your condition */) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.indentationLevel = 4;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.indentationLevel = 0;
}