UITableView自定义单元格仅显示所有单元格的一行文本
我正在使用自定义单元格使用Facebook Graph API显示Facebook页面的文章。我得到了JSON数据,并且我将所有帖子转换为msgArray,然后将这个msgArray传递给我的视图控制器,其中我有我的表视图,然后使用此msgArray来填充自定义单元格中的数据。但是,当我在自定义单元格中显示数据时,它仅在表格视图的所有单元格中加载标签(postText:labelName)中的一行文本,并且当我向上或向下滚动时,几乎没有单元格显示完整的文本和少数只显示一行文本,这取决于我等待文本加载多少。UITableView自定义单元格仅显示所有单元格的一行文本
下面是我的表视图代码:
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [msgArray count];
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *[email protected]"Cell";
CustomCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//UITableViewCell *tablecell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
//if(cell==nil)
// {
// cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//}
cell.postText.lineBreakMode=NSLineBreakByWordWrapping;
cell.postText.numberOfLines=0;
[cell.postText sizeToFit];
cell.postText.text=[msgArray objectAtIndex:indexPath.row];
return cell;
}
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//This function is calculating my height for each row depending upon text length which is correct.
NSString *text=[msgArray objectAtIndex:indexPath.row];
CGSize constraint=CGSizeMake(300,20000.0f);
CGSize size=[text sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height=MAX(size.height,44.0f);
return height+(100*2);
}
还有一两件事,如果我使用默认的UITableViewCell,并正确设置文本cell.textLabel.text=[msgArray objectatIndex:indexPath.row];
,它显示的数据。
请帮我解决这个问题。提前致谢。
您是否尝试将其numberOfLines属性更改为0?默认情况下,它是1
cell.textLabel.numberOfLines = 0;
是的请检查我的代码,我写了cell.postText.numberOfLines = 0; –
一件事我删除了这个自定义单元格代码,并在运行时添加了一个UILabel,并在该标签上添加了我的文本,并使用[cell.contentMode addSubview:lbl]将其添加到单元格中; 它显示了我的单元格空白,我不知道为什么我的标签不可见在UITableViewCell上。我必须做一些非常愚蠢的事情,而我却无法得到。 –
您是否尝试调整标签高度和字体大小?尝试使用较小的字体大小,并将标签高度设置为单元格高度 –
你必须调整postText高度,以及...设置的锚相对于父postText,身高TextView的调整也应该调整......还用CGSize约束= CGSizeMake( 300,MAXFLOAT); – faiziii
Faiziii,我无法得到。你可以在这里写一行代码,它会帮助很多。 –
Faiziii,我无法得到。你可以在这里写一行代码,它会帮助很多。 但是你在谈论调整标签的大小,我也做到了。它没有工作。 CGSize maximumLabelSize = CGSizeMake(296,9999); CGSize expectedLabelSize = [[msgArray objectAtIndex:indexPath.row] sizeWithFont:lblPost.font constrainedToSize:maximumLabelSize lineBreakMode:lblPost.lineBreakMode]; CGRect newFrame = lblPost.frame; newFrame.size.height = expectedLabelSize.height; lblPost.frame = newFrame; –