2012-08-23 239 views
0

我有uitableviewcells一个UITableView但是我的一些文字是这么久其在同行的细胞,在那里我的附件蜱视图会选择时要边..设置宽度

我我目前正在包装文本,如果它超出了宽度,它会到第二行。但是我希望有一个很好的方法来限制这个UITableViewCell标签的宽度。

+0

你想在给定的宽度的标签适合文本? –

+0

没了遗憾,在(标签为一个固定的宽度)动态文本 – HurkNburkS

回答

0

我认为你需要子类UITableViewCell覆盖-layoutSubviews ...呼叫super,然后适当地框架你的标签。限制我有一些帮助,但我还没有在iOS上使用它们。 (假设问题在于内置布局功能使标签尽可能宽以适应您的文本)

0

有两种方法可以这样做。

1)创建一个自定义的UITableViewCell类,添加您的UILabels任何位置&大小你想要的。然后在cellForRowAtIndexPath方法中使用它。

2)否则,请不要使用UITableViewCell的默认UILabels。 textLabel & detailTextLabel,将您自己的UILabels添加到单元格中,无论您想要的大小为&。

+0

实际上,#2是不坏 - 只要你喜欢,你的_autoresizingMask_设置为'UIViewAutoresizingMaskFlexibleWidth',虽然这可能还需要子类,你可以定位你的自定义标签。 – nielsbot

1

在你的cellForRowAtIndexPath函数中:当你第一次创建你的:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0]; 
} 

现在指定的UITableViewCell将有多大,这样做,在你的heightForRowAtIndexPath功能:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *str = [[arrTexts objectAtIndex:indexPath.row]; // filling text in label 
    CGSize maximumSize = CGSizeMake(300, 100); // change width and height to your requirement 
CGSize strSize = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17] constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap] //dynamic height of string depending on given width to fit 

return (10+strSize.height+10) // caculate on your bases as u have string height 
} 
+0

我不能得到这个工作,它只是不断增加我的单元格的高度,但并没有影响任何与我的宽度..仍在努力也许我拍过的东西了..但至今没有运气。 – HurkNburkS

1

cellForRowAtIndexPath设置的textLabel

宽度
cell.textLabel.frame = CGRectMake(cell.textLabel.frame.origin.x, cell.textLabel.frame.origin.y, 280, cell.textLabel.frame.size.height); 

这就是所有。