我正在尝试使 UITableViewCell 的 textlabel 中的文本从单元格的右侧包裹大约 30 个像素。然而,它从右边最多大约 5 或 10 个像素。我尝试编辑我在 cellForRowAtIndex 中设置的所有值,但是唯一改变的是单元格的高度,而不是单元格的 textLabel 有多大是的,我无法找到错误,希望这里有人能看到错误。
我一直在做进一步的测试,我发现如果我使用sectionIndexTitlesForTableView
UItableViewCell textLabel 将减小到下面代码中概述的正确大小。但是如果我禁用此方法,那么它不会注意我试图在下面的代码中设置宽度。
这是我的代码如下。
//These Constants are used for dynamic cell height
#define FONT_SIZE 18.0f
#define CELL_CONTENT_WIDTH 290.0f
#define CELL_CONTENT_MARGIN 10.0f
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell using custom cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.textLabel setLineBreakMode:UILineBreakModeWordWrap];
[cell.textLabel setMinimumFontSize:FONT_SIZE];
[cell.textLabel setNumberOfLines:0];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
[cell.textLabel setTag:1];
[[cell contentView] addSubview:cell.textLabel];
}
//Customize cell here
cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection
//Replaces previously selected cells accessory view (tick)
if ([indexPath isEqual:oldCheckedIndexPath]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
//Display cells with data that has been sorted from startSortingTheArray
NSMutableArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]];
NSString *key = [keys objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
//Applise current key value to cell text label
[cell.textLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 25.0f))];
[cell.textLabel setText:key];
return cell;
}
//Cell size for word wrapping So the user can see all of the details of a submodel etc.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//Display cells with data that has been sorted from startSortingTheArray
NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]];
NSString *key = [keys objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 25.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}