uitableview - 折叠不适当的部分

  显示原文与译文双语对照的内容
0 0

我有一个可以在标题部分折叠的UITable 。 我需要标题节在它们中有唯一的文本。

我可以使用 cell.textlabel.text = @"";,这可以正常工作,但是我想向单元格添加更多的标签,并定位这些 i.e

 lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
 lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
 lbl123.textColor = [UIColor blackColor];
 lbl123.backgroundColor = [UIColor clearColor];
 lbl123.highlightedTextColor = [UIColor whiteColor];
 lbl123.opaque = NO;
 lbl123.adjustsFontSizeToFitWidth = TRUE;
 lbl123.numberOfLines = 0;
 lbl123.lineBreakMode = UILineBreakModeWordWrap;
 [cell addSubview:lbl123];
 lbl123.text = @"test";

但是当我将它添加到"cellForIndexPath"上的部分时,它会在表格的下方。

为什么会发生这种情况?

下面的完整代码:-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 NSString *cellValue = 0;
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
 if (cell == nil)
 {
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
 reuseIdentifier:@"MyIdentifier"];
 }
 if (tableView.tag == 2)
 {
 UILabel *lbl123;
 if ([self tableView:tableView canCollapseSection:indexPath.section])
 {
 cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
 if (!indexPath.row)
 {
 cell.textLabel.backgroundColor = [UIColor clearColor];
 UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
 [bgView setBackgroundColor:[UIColor blackColor]];
 [bgView setAlpha:0.3];
 [cell setBackgroundView:bgView];
 lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
 lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
 lbl123.textColor = [UIColor blackColor];
 lbl123.backgroundColor = [UIColor clearColor];
 lbl123.highlightedTextColor = [UIColor whiteColor];
 lbl123.opaque = NO;
 lbl123.adjustsFontSizeToFitWidth = TRUE;
 lbl123.numberOfLines = 0;
 lbl123.lineBreakMode = UILineBreakModeWordWrap;
 [cell addSubview:lbl123];
 lbl123.text = @"";
 if(indexPath.section == 1)
 {
 cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
 cell.textLabel.text = [NSString stringWithFormat:@"My Results Overview:"];
 cell.textLabel.textColor = [UIColor whiteColor];
 cell.textLabel.numberOfLines = 3;
 cell.textLabel.opaque = YES;
 UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 80)];
 av.backgroundColor = [UIColor clearColor];
 av.opaque = NO;
 av.image = [UIImage imageNamed:@"Blue bar 720x115.png"];
 cell.backgroundView = av;
 }
 if(indexPath.section == 2)
 {
 cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
 cell.textLabel.text = [NSString stringWithFormat:@"Result1:"];
 cell.textLabel.textColor = [UIColor blackColor];
 cell.textLabel.numberOfLines = 3;
 cell.textLabel.opaque = YES;
 UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
 av.backgroundColor = [UIColor clearColor];
 av.opaque = NO;
 if ([confidenceCOL isEqualToString:@"Red"])
 {
 av.image = [UIImage imageNamed:@"redPanel.png"];
 }
 if ([confidenceCOL isEqualToString:@"Green"])
 {
 av.image = [UIImage imageNamed:@"greenPanel.png"];
 }
 if ([confidenceCOL isEqualToString:@"Purple"])
 {
 av.image = [UIImage imageNamed:@"purplePanel.png"];
 }
 cell.backgroundView = av;
 lbl123.text = @"Test";
 }
 if(indexPath.section == 3)
 {
 cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
 cell.textLabel.text = [NSString stringWithFormat:@"Result2:"];
 cell.textLabel.textColor = [UIColor blackColor];
 cell.textLabel.numberOfLines = 3;
 cell.textLabel.opaque = YES;
 UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
 av.backgroundColor = [UIColor clearColor];
 av.opaque = NO;
 if ([ambitionCOL isEqualToString:@"Red"])
 {
 av.image = [UIImage imageNamed:@"redPanel.png"];
 }
 if ([ambitionCOL isEqualToString:@"Green"])
 {
 av.image = [UIImage imageNamed:@"greenPanel.png"];
 }
 if ([ambitionCOL isEqualToString:@"Purple"])
 {
 av.image = [UIImage imageNamed:@"purplePanel.png"];
 }
 cell.backgroundView = av;
 lbl123.text = @"TestING!";
 }
 if ([expandedSections containsIndex:indexPath.section]){}
 else{}
 }
 else
 {
 if(indexPath.section == 2)
 {
 cellValue = Result1Moretext;
 }
 if(indexPath.section == 3)
 {
 cellValue = Result2Moretext;
 }
 static NSUInteger const k1 = 1;
//Declare references to the subviews which will display the data.
 UILabel *lbl1 = nil;
 static NSString *kCellID = @"CellID";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
 if (cell == nil) {
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
 lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 300)];
 lbl1.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
 lbl1.tag = k1;
 lbl1.textColor = [UIColor blackColor];
 lbl1.backgroundColor = [UIColor clearColor];
 lbl1.highlightedTextColor = [UIColor whiteColor];
 lbl1.opaque = NO;
 lbl1.adjustsFontSizeToFitWidth = TRUE;
 lbl1.numberOfLines = 0;
 lbl1.lineBreakMode = UILineBreakModeWordWrap;
 [cell.contentView addSubview:lbl1];
 }else {
 lbl1 = (UILabel *)[cell.contentView viewWithTag:k1];
 }
 cell.accessoryView.tag = indexPath.row;
 lbl1.text = [NSString stringWithFormat:@"%@", cellValue];
 return cell;
 }
 }
 else
 {
 }
 }

谢谢

时间:原作者:1个回答

0 0

不要忘记UITableViews在中被重用以保持内存使用率低。 现在的情况是你正在制造一个单元,加上标签,然后在细胞滚动之后,屏幕滚动到一个reuse池,然后再将它添加到一个新的单元中,然后再继续。

在创建新单元格时,应该只创建所有标签等 ,然后在代码的其余部分设置所有标签。

你可以使用视图标记在以后再次查找标签视图。

比如,

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil)
{
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
 reuseIdentifier:@"MyIdentifier"];
 lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
 lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
 lbl123.textColor = [UIColor blackColor];
 lbl123.backgroundColor = [UIColor clearColor];
 lbl123.highlightedTextColor = [UIColor whiteColor];
 lbl123.opaque = NO;
 lbl123.adjustsFontSizeToFitWidth = TRUE;
 lbl123.numberOfLines = 0;
 lbl123.lineBreakMode = UILineBreakModeWordWrap;
 [cell addSubview:lbl123];
 lbl123.text = @"";
 lbl123.tag=123;
} else {
 lbl123 = (UILabel *)[cell viewWithTag:123];
}
if(indexPath.section == 2)
{
 lbl123.text = @"Test";
}
原作者:
...