ios - 我想用CellFrame添加View

时间:2015-07-14 03:48:56

标签: ios addsubview tableviewcell

我在TableViewCell中添加了这样的视图。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell * cell = [[UITableViewCell alloc]init];
  UIView * view = [[UIView alloc] initWithFrame:cell.frame];
  view.backgroundColor = [UIColor redColor];
  [cell addSubview: view];

}

我想添加View with Cell Frame

但结果是 enter image description here 我该怎么办?

1 个答案:

答案 0 :(得分:0)

如果您创建单独的类并在其上添加视图,那么它将显示在完整单元格上,但如果您不想为单元格创建单独的类,则使用以下代码。 因为我将视图宽度设置为与表视图宽度相同

 UITableViewCell * cell = [[UITableViewCell alloc]init];
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.height, cell.frame.size.height)];
        view.backgroundColor = [UIColor redColor];
        cell.backgroundColor =[UIColor purpleColor];
        [cell addSubview: view];

我希望它能帮到你!

相关问题