0

我正在尝试根据 tableview 的单元格将子视图添加到视图中。我已经完成了这段代码

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VerseCell *cell = (VerseCell *)[tableView cellForRowAtIndexPath:indexPath];

    rowSelected = indexPath.row;

    CGRect  cellRect = cell.frame;

    UIView *cellView =[[UIView alloc]initWithFrame:CGRectMake(cellRect.origin.x+4, cellRect.origin.y+15, cellRect.size.width-20, 70)];

  UIImageView* background =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"FavoritePanelBackground"]];
    background.frame =CGRectMake(cellRect.origin.x+4, cellRect.origin.y+15, cellRect.size.width-20, 70);
    [cellView addSubview:background];
    [cellView setBackgroundColor:[UIColor redColor] ];
    UIButton * favoritsB = [[UIButton alloc]initWithFrame:CGRectMake(cellRect.origin.x+10, cellRect.origin.y+15, 60, 40)];
    [favoritsB setBackgroundImage:[UIImage imageNamed:@"MofadalatNormalIcon.png"] forState:UIControlStateNormal];
    [favoritsB setBackgroundImage:[UIImage imageNamed:@"MofadalatTappedIcon.png"] forState:UIControlStateHighlighted];
  [favoritsB addTarget:self action:@selector(favorite) forControlEvents:UIControlEventTouchUpInside];

    [cellView addSubview:favoritsB];

    cellView=nil;

}

问题是..此代码仅在第一行按预期工作..当单击下一个单元格时,它会在预期位置显示主 UIVIew(我用红色背景标记),而它的子视图是真的走得很远......所以我在这里做错了什么......如果这不起作用,还有另一种方法可以解决我正在做的事情吗?

4

1 回答 1

0

你想在哪里添加这些视图?在表格视图单元格内?如果是这样,您永远不会将 cellView 添加为单元格的子视图。如果这就是您要执行的操作,请在 cellView = nil 上方添加此行:

[单元格添加子视图:单元格视图];

于 2012-11-14T19:26:50.447 回答