Hur*_*rkS 2 iphone uitableview ios
我试图让它成为我的UITableViewCell文本标签中的文本从单元格的右侧包裹大约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);
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*att 10
单元格的layoutSubviews方法将覆盖您尝试在cellForRow中执行的任何操作(或大多数其他地方).最好的解决方案是继承UITableViewCell并覆盖layoutSubviews.确保调用super,然后自定义文本标签框.
编辑:
只需在实现和使用之上包含此(当然已修改)
CustomTableViewCell *cell = [tableView dequeReusableCell...
Run Code Online (Sandbox Code Playgroud)
和
cell = [[CustomTableViewCell alloc] initWithStyle...
Run Code Online (Sandbox Code Playgroud)
代替.不需要额外的.xibs.
@interface CustomTableViewCell : UITableViewCell
@end
@implementation CustomTableViewCell
- (void)layoutSubviews
{
[super layoutSubviews];
cell.textLabel.frame = theFrameYouWant;
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6354 次 |
| 最近记录: |