我将为您发布的代码发布示例修复。它可以扩展以处理更多视图。
步骤是:
- 在您的 CustomCell 类中创建一个方法来处理整个设置(例如setupWithItems::)
- 一旦你有一个单元格cellForRowAtIndexPath:(在出队或创建它之后),你应该调用setupWithItems:单元格应该显示的新项目列表。
- 在您的setupWithItems:实现中,请确保从其父视图中删除 UISegmentedControl。您可以轻松地做到这一点,因为分段控件存储为自定义单元格的属性。
- 在您的setupWithItems:实现中,创建一个新的 UISegmentedControl 并将其添加到 CustomCell 的视图层次结构中。
示例代码:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:kSomeIdentifier];
    if (!cell)
    {
        // Create a new cell
    }
    NSArray* currentCellItems = [self cellItemsForRow:indexPath.row];
    [cell setupWithItems:currentCellItems];
    return cell;
}
在您的 CustomCell 子类中:
- (void)setupWithItems:(NSArray*)items
{
    if (self.segmentedControl)
    {
        [self.segmentedControl removeFromSuperView];
        self.segmentedControl = nil;
    }
    // More setup...
    UISegmentedControl *btn = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:btn_title]];
    // set various other properties of btn
    [cell.contentView addSubview:btn];
}