0

我想知道如何调整表格视图单元格的大小,并查看所有消息我的应用程序正在从服务器上的 xml 获取消息,然后显示它们,但如果消息比文本字段长,它只会显示3个点是我的代码:

 - (NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section {
return ( messages == nil ) ? 0 : [messages count];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 37;
}

- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:@"ChatListItem"];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatListItem" owner:self options:nil];
    cell = (UITableViewCell *)[nib objectAtIndex:0];
}

NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
textLabel.text = [itemAtIndex objectForKey:@"text"];
UILabel *stextLabel = (UILabel *)[cell viewWithTag:3];
stextLabel.text = [itemAtIndex objectForKey:@"text"];

UILabel *userLabel = (UILabel *)[cell viewWithTag:2];
userLabel.text = [itemAtIndex objectForKey:@"user"];
UILabel *suserLabel = (UILabel *)[cell viewWithTag:4];
suserLabel.text = [itemAtIndex objectForKey:@"user"];

if ([usernameText.text isEqualToString:userLabel.text]){
    UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"chat1@2x.png"]];
    [cell setBackgroundView:img];
    [img release];

    textLabel.hidden=NO;
    stextLabel.hidden=YES;

    userLabel.hidden=NO;
    suserLabel.hidden = YES;


}
else{
    UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"chat2@2x.png"]];
    [cell setBackgroundView:img];
    [img release];

    textLabel.hidden=YES;
    stextLabel.hidden=NO;

    userLabel.hidden=YES;
    suserLabel.hidden = NO;
}

return cell;

}

谢谢

4

1 回答 1

1

你必须像这个逻辑一样通过 heightForRowAtIndexPath (代码还没有准备好):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
NSString *text = [itemAtIndex objectForKey:@"text"];
if(text is larger then what ever you want) {
    return yourSpecificCellHeight;
}
return 37;

}

要计算文本的大小,请参阅stackOverflow 上的这篇文章

于 2012-11-15T07:21:11.487 回答