UITableViewCell在重新加载时移动内容

时间:2013-07-08 09:13:33

标签: iphone ios uitableview

我使用自定义UITableViewCell ,顶部是 UILabel ,标签下方是 UIButton 另一个UILabel < / strong>,就在这个按钮下。

最终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;

}

1 个答案:

答案 0 :(得分: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) {

    }
 }
}