Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 根据UITableViewCell CGRect添加子视图_Ios_Uitableview_Tableview - Fatal编程技术网

Ios 根据UITableViewCell CGRect添加子视图

Ios 根据UITableViewCell CGRect添加子视图,ios,uitableview,tableview,Ios,Uitableview,Tableview,我正在尝试根据tableview的单元格将子视图添加到视图中。。我已经完成了这个代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { VerseCell *cell = (VerseCell *)[tableView cellForRowAtIndexPath:indexPath]; rowSelected = indexPath.ro

我正在尝试根据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(我用红色背景标记了它),而它的子视图则会非常低。。。那么我做错了什么。。如果这样做不起作用,还有其他方法吗?

您想在哪里添加这些视图?在表格视图单元格内?如果是这样,您永远不会将cellView添加为cell的子视图。如果您正试图这样做,请在cellView=nil上方添加这一行:


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

实际上,我添加它作为self.view的子视图,而不是表本身