【发布时间】:2014-11-06 04:34:12
【问题描述】:
我是 Xcode 和 Objective-C 的菜鸟,我需要帮助来根据标签中有多少字符来制作动态表格视图单元格的高度。所以它会是这样的: 如果 textLabel char 大于 10,它会将 listHeight 调整为 50 否则,如果 textLabel char 大于 20,它会将 listHeight 调整为 70 等等……
这就是我的编码方式:
NSLong *text;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"ChatTableViewCell";
ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
Chat * chatObject;
chatObject = [chatArray objectAtIndex:indexPath.row];
cell.nameChat.text = chatObject.name;
cell.messageChat.text = chatObject.message;
cell.messageChat.lineBreakMode = UILineBreakModeTailTruncation;
cell.messageChat.numberOfLines = 0;
cell.dateChat.text = chatObject.time_entry;
//attached foto
NSString *setPhoto=chatObject.photo;
[cell.imageChat setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost/summit/www/media/user/photo/%@",setPhoto]]]]];
cell.imageChat.layer.cornerRadius=cell.imageChat.frame.size.height /2;;
cell.imageChat.layer.masksToBounds = YES;
cell.imageChat.layer.borderWidth = 0;
if (cell.imageChat.image==nil) {
cell.imageChat.image=[UIImage imageNamed:@"profile.png"];
}
text=[cell.messageChat.text length];
return cell;
}
我正在尝试这个,但不工作:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height;
if(text>=10){
height=100;
}
else
height=70;
return height;
}
请帮助我,我很想这样做@_@提前谢谢
【问题讨论】:
-
您提供了一个构造单元格的示例,但未定义其高度。你实现了
tableView:heightForRowAtIndexPath:方法了吗?你试过什么?此外,您真的希望高度取决于字符数还是要确保单元格足够大以容纳其中包含的任何文本? -
我有方法但是现在它只包含静态值 return 70;
-
我认为您需要更好地定义您认为可以计算正确单元格高度的规则,即使您不确定如何在代码中表达它们。否则,这里的任何答案都只是对您尝试做的事情的猜测。您的单元格似乎包含许多文本,其中哪些会影响单元格的高度?
标签: ios objective-c uitableview height