UITableView里面的视图背景,在UITableViewCell里面

时间:2013-04-26 06:54:59

标签: objective-c ipad ios5 uitableview

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

我有一个外部UITableViewCell的自定义后台视图。

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

当我选择tableviewcell时,内部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;
}

子桌面视图被旋转,因为我想要水平的桌面视图。

任何人都知道这是什么解决方案?

1 个答案:

答案 0 :(得分:1)

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

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

相关问题