设置UITableViewCell文本的宽度

我有一个与uitableviewcells uitableview但是我的一些文本是这么长的权利到细胞的边缘,在我的附件滴答视图将被选中时。

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

在你的cellForRowAtIndexPath:函数中。 你第一次创build你的单元格:

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 } 

cellForRowAtIndexPath设置textLabel的宽度

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

就这样。

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

有两种方法可以做到这一点。

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

2)否则,不要使用UITableViewCell的默认UILabel。 textLabel&detailTextLabel,在你想要的位置和大小的单元格中添加你自己的UILabel。