【问题标题】:Background of View inside UITableView which is inside UITableViewCellUITableView 内的视图背景,位于 UITableViewCell 内
【发布时间】:2013-04-20 06:32:45
【问题描述】:

我在 UITableViewCell 中有一个 UITableView。内部表格视图大小为 5,每个单元格内容视图具有不同的背景颜色,并且是可编辑的。我能够重新排序内部表格视图的表格视图单元格。

我有一个外部 UITableViewCell 的自定义背景视图。

 UIView *selectedBackgroundView = [[UIView alloc] init];
 selectedBackgroundView.layer.borderColor = [[UIColor grayColor] CGColor];
 selectedBackgroundView.layer.borderWidth = 1;
 selectedBackgroundView.backgroundColor = [UIColor clearColor];
 cell.selectedBackgroundView = selectedBackgroundView;

当我选择tableviewcell时,inner tableview的tableviewcells的bg颜色变为白色。

//父TableView的CellForRow代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    TableViewCellCustom *cell = (TableViewCellCustom *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TableViewCellCustom" owner:self options:nil];
        cell = _tableViewCellCustom;

        UIView *selectedBackgroundView = [[UIView alloc] init];
        selectedBackgroundView.layer.borderColor = [[UIColor grayColor] CGColor];
        selectedBackgroundView.layer.borderWidth = 1;
        selectedBackgroundView.backgroundColor = [UIColor clearColor];
        cell.selectedBackgroundView = selectedBackgroundView;
    }

    CustomPosition *customPosition = [_filteredArray objectAtIndex:indexPath.row];

    [cell setProductsArray: customPosition.productsArray];//Sets the products and reloads child tableview

    return cell;
}

//子表视图的CellForRow

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    TableViewCellProduct *cell = (TableViewCellProduct *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TableViewCellProduct" owner:self options:nil];
        cell = _tableViewCellProduct;

        cell.viewProduct.transform = CGAffineTransformRotate(cell.viewProduct.transform, M_PI_2);
    }

    switch (indexPath.row) {
        case 0:
            cell.viewProduct.backgroundColor = [UIColor redColor];
            break;

        case 1:
            cell.viewProduct.backgroundColor = [UIColor greenColor];
            break;

        case 2:
            cell.viewProduct.backgroundColor = [UIColor blueColor];
            break;

        case 3:
            cell.viewProduct.backgroundColor = [UIColor yellowColor];
            break;

        case 4:
            cell.viewProduct.backgroundColor = [UIColor grayColor];
            break;

        default:
            break;
    }

    Product *product = [_productsArray objectAtIndex:indexPath.row];
    cell.lblProductName.text = product.name;

    return cell;
}

子表视图被旋转,因为我想要水平表视图。

谁知道这个问题的解决方法是什么?

【问题讨论】:

  • 发布 cellForRowAtIndexPath 方法代码。

标签: objective-c ipad ios5 uitableview


【解决方案1】:

选择表格视图单元格时,框架会遍历所有子视图并将其背景颜色更改为UIColor clearColor。这样才能正确看到表格视图单元格的背景颜色。通常这就是你想要的。

如果不是你想要的,你需要继承UITableViewCell,这样在选择之后,你可以恢复所有子视图的背景颜色。或者完全自己管理选择结果。

【讨论】:

  • 谢谢。我没有在 cellForRowAtIndexPath: 中提供 selectionBackground,而是在 UITableViewCell 子类的 setSelected 方法中设置它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
相关资源
最近更新 更多