-3

我正在使用一个自定义 UITableViewCell ,顶部有一个UILabel ,一个UIButton就在标签下面,另一个 UILabel就在这个按钮下面。

最终的 UILabel 最初是隐藏的,当调用按钮动作时会打开。所有标签和单元格的高度都是动态的。

当我选择按钮时,单元格将其内容视图转移到。中央

我需要单元格的内容从原点本身开始。

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


    static NSString *CellIdentifier = @"ReferenceCell";
    ReferenceiPadTableCell *cell = (ReferenceiPadTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ReferenceiPadTableCell" owner:self options:Nil];
        cell = [nib objectAtIndex:0];
        cell.accessoryView = nil;
    }
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    NSDictionary *referenceDictionary = [self.referencesList objectAtIndex:indexPath.row];

    NSString *nameString = [referenceDictionary objectForKey:@"name"];


    CGSize labelSize = [nameString sizeWithFont:[UIFont systemFontOfSize:14]
                              constrainedToSize:CGSizeMake(listTableView.frame.size.width - 15, 125)
                                  lineBreakMode:UILineBreakModeWordWrap];
    labelSize.height =  labelSize.height > 96? labelSize.height : 96;

    cell.l1.text = nameString;
    cell.l1.frame = CGRectMake(cell.l1.frame.origin.x, 6, cell.l1.frame.size.width, labelSize.height);

    cell.button.tag = [[referenceDictionary objectForKey:@"sort"] intValue];
    [cell.button addTarget:self action:@selector(showList:) forControlEvents:UIControlEventTouchUpInside];
    cell.l2.hidden = YES;


    cell.button.frame = CGRectMake(cell.button.frame.origin.x, cell.l1.frame.origin.y+ labelSize.height + 1, cell.l1.frame.size.width, cell.l1.frame.size.height);

    if ([self.showList objectForKey:[NSString stringWithFormat:@"showList-%d", indexPath.row]] )
    {
        cell.l2.hidden = NO;
        NSDictionary *dic = [self.abstractList objectAtIndex:indexPath.row];


        NSString *abtString = [dic objectForKey:@"name"];
        CGSize absSize = [abtString sizeWithFont:[UIFont systemFontOfSize:14]
                               constrainedToSize:CGSizeMake(tableView.frame.size.width - 15, 125)
                                   lineBreakMode:UILineBreakModeWordWrap];
        cell.l2.frame = CGRectMake(cell.button.frame.origin.x, cell.button.frame.origin.y+ cell.button.frame.size.height + 3, cell.button.frame.size.width, absSize.height);
        cell.l2.text = abtString;
    }

    return cell;

}
4

1 回答 1

0

它不会改变内容,而是在 UITableViewCell 上添加了更多子视图,因为您没有很好地使用重用单元格概念。所以使用这样的概念

  -(UITableViewCell *)tableView:(UITableView *)tableView reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *) indexPath
   {
UITableViewCell *cell=[[UITableViewCell alloc]     initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

UILabel *lbl=[[UILabel alloc]init];
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
lbl.backgroundColor=[UIColor clearColor];
lbl.textAlignment=NSTextAlignmentCenter;

UIImageView *imgView=[[UIImageView alloc]init];
UIImageView *imgView1=[[UIImageView alloc]init];
UIImageView *imgView2=[[UIImageView alloc]init];
UIImageView *imgView3=[[UIImageView alloc]init];
 UIImageView *imgView4=[[UIImageView alloc]init];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
      lbl.font=[UIFont fontWithName:@"Marker Felt" size:20];
}
else
{
      lbl.font=[UIFont fontWithName:@"Marker Felt" size:30];
}
[cell.contentView addSubview:lbl];

[cell.contentView addSubview:imgView];

[cell.contentView addSubview:imgView1];

[cell.contentView addSubview:imgView2];

[cell.contentView addSubview:imgView3];
[cell.contentView addSubview:imgView4];
return  cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell=[self tableView:tableView reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    }
 }
}
于 2013-07-08T10:15:23.777 回答