我创建了一个 UITableViewController,它根据包含的文本量计算每行的高度。计算行高的代码如下所示:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
Message *message = [[[[SessionData sharedSessionData] sessions] objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
CGSize size = [[message text] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height + (CELL_CONTENT_MARGIN * 2) + 38;
}
正如您可能注意到的,我使用了一个单独的类作为 UITableViewDatasource,称为 SessionData。在 SessionData 类中,我绘制行。每行都有一个顶部图像、中心图像(根据行高重复)和底部图像。我的问题如下:中心图像比底部和顶部图像更暗。我的猜测是这与图像的重复性质有关,因为顶部和底部的图像绘制得很好。以下代码是我的 [tableView:cellForRowAtIndexPath:] 消息的一部分:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// other stuff here, most likely not related to my issue ...
Message *message = [[sessions objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [[message text] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setText:[message text]];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN - 5.0f, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), size.height)];
[label setBackgroundColor:[UIColor clearColor]];
viewTop = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"im1 top.png"]];
viewMid = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"im1 mid.png"]];
viewBot = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"im1 bot.png"]];
[viewTop setFrame:CGRectMake(0.0, 0.0, 300.0, 17.0)];
[viewMid setFrame:CGRectMake(0.0, 17.0, 300.0, size.height - 10)];
[viewBot setFrame:CGRectMake(0.0, 17.0 + size.height - 10, 300.0, 31.0)];
[viewTop setBackgroundColor:[UIColor clearColor]];
[viewMid setBackgroundColor:[UIColor clearColor]];
[viewBot setBackgroundColor:[UIColor clearColor]];
[cell.backgroundView setBackgroundColor:[UIColor clearColor]];
[((UIImageView *)cell.backgroundView) addSubview:viewTop];
[((UIImageView *)cell.backgroundView) addSubview:viewMid];
[((UIImageView *)cell.backgroundView) addSubview:viewBot];
[[cell backgroundView] addSubview:label];
return cell;
}
我的猜测是我需要为顶部、中心和底部图像视图创建一个具有总大小(宽度、高度)的 containerView,并将 containerView 添加到单元格中,但这似乎不起作用。任何人都知道如何解决这个问题?
请注意:我的 UITableViewController 有一个渐变作为背景图像。所以顶部/中心/底部图像绘制在背景渐变的顶部。也许这也是问题的一部分。