gpt4 book ai didi

xcode - 当附件为UITableViewCellAccessoryCheckmark时如何在UITableViewCell中居中放置文本

转载 作者:行者123 更新时间:2023-12-04 05:04:44 25 4
gpt4 key购买 nike

当我将attachmentView设置为UITableViewCellAccessoryCheckmark时,我试图使文本在UITableViewCell中居中。我还在本类(class)中使用 Storyboard 。

这是我不需要的屏幕截图。

如何阻止文本向左缩进?

我的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:
- (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;
}

最佳答案

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

关于xcode - 当附件为UITableViewCellAccessoryCheckmark时如何在UITableViewCell中居中放置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10841693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com