Uitableview 如何仅在第一个UITableCellView中添加UIScrollView?

Uitableview 如何仅在第一个UITableCellView中添加UIScrollView?,uitableview,uiscrollview,Uitableview,Uiscrollview,我在uiviewcontroller中有一个uitableview,我在viewload事件中创建了一个scrollview。 我将它添加到tableview的第一个单元格中。但是我滚动tableview,在5个单元格通过后,它会显示多个滚动视图 这是代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSt

我在uiviewcontroller中有一个uitableview,我在viewload事件中创建了一个scrollview。 我将它添加到tableview的第一个单元格中。但是我滚动tableview,在5个单元格通过后,它会显示多个滚动视图

这是代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"customCell";

    DetailCellViewController *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nibObjects =[[NSBundle mainBundle] loadNibNamed:@"DetailCellView" owner:nil options:nil];
        for (id currentObject in nibObjects)
        {
            if ([currentObject isKindOfClass:[DetailCellViewController class]])
            {
                cell = (DetailCellViewController *) currentObject;
            }
        }
    }

    if (indexPath.row==0) {
        [cell.contentView addSubview:scrollView];
    }
    else {
        NSMutableDictionary *dictionary=[catData objectAtIndex:indexPath.row-1];
        NSString *title =[dictionary objectForKey:@"title"]];
    [cell.catTitle setText:title];
    }
    return cell;
}

在哪种情况下,我应该添加和删除scrollview?

我猜您将获得一个已包含UIScrollView的已出列UITableViewCell。如果您真的关心分隔单元格类型,我建议您将其设置为至少有两个CellIdentifier字符串。(有时,我会设置一个UITableView来处理4+种不同的单元格类型;一旦超出了一种单元格类型,它就基本相同了。)


我建议的解决方案:(见下面代码的解释)


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"bodyCell";
    static NSString *HeaderIdentifier = @"headerCell";

    UITableViewCell *cell;

    // I break this up into 3 sections
    // #1. Try to dequeue a cell
    // #2. Create a new cell (if needed)
    // #3. Set up the cell I've created

    // Step 1: Try to dequeue a cell
    if ([indexPath section] == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:HeaderIdentifier];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }

    // At this point, we may or may not have a cell to use, 
    // so we check for the cell's value being equal to 'nil' 
    // and create a new cell if it is

    // Step 2: Create a new cell (if needed)
    if (cell == nil) {
        // Again, here we check for section to determine 
        // what kind of cell we want
        if ([indexPath section] == 0) {
            // We have the "header"/first cell
            // Option 1
            cell = [[ScrollViewTableViewCell alloc] init];
            // Option 2 (this assumes you've got a xib named 
            // ScrollingTableViewCell along with a class property 
            // named headerCell and have properly wired it up in 
            // Interface Builder)

            [[NSBundle mainBundle] loadNibNamed:@"ScrollingTableViewCell" 
                                          owner:self 
                                        options:nil];
            cell = [self headerCell];
            [self setHeaderCell:nil];

        } else {
            // We have a "body" cell (anything other than the first cell)
            // Option 1
            cell = [[BodyTableViewCell alloc] init];
            // Option 2 (again, assuming you've set things up properly)
            [[NSBundle mainBundle] loadNibNamed:@"BodyTableViewCell" 
                                          owner:self 
                                        options:nil];
            cell = [self bodyCell];
            [self setBodyCell:nil];
        }
    }

    // At this point, whether dequeued or created 
    // new, 'cell' should be populated
    // Again, we check for section and set up the cell as appropriate
    if ([indexPath section] == 0) {
        // Set up the header (UIScrollView) cell as appropriate
        // This is where you would add the UISCrollView to your cell
        // (if you haven't set up the UIScrollView through Interface Builder)
    } else {
        // Set up the "body" cell as appropriate
    }

    return cell;
}
注意:我强烈建议使用上面的选项2。到目前为止,在使用自定义/非标准UITableViewCells时,我发现最好的结果是创建自己的UITableViewCell子类,并使用xib。以下是实现这一目标的步骤:

  • 创建UITableViewCell的子类(我们将调用您的ScrollingTableViewCell.h/.m)
  • 将ScrollingTableViewCell类向前/导入到UITableViewController(或承载UITableView的UIViewController)中
  • 创建一个ScrollingTableViewCell类型的类属性(我们称您的ScrollingCell)
  • 创建一个视图(新文件>用户界面>视图)(我们将调用您的ScrollingTableViewCell.xib)
  • 删除xib中的库存视图项,并将其替换为UITableViewCell项。

    备选方案#4/5
  • 创建一个空的Xib
  • 添加UITableViewCell项。

    非常重要
  • 在xib中,文件的所有者是ViewController,而不是UITableViewCell。单元格的类是ScrollingTableViewCell
  • 在IB中,将ViewController的ScrollingCell属性连接到UITableViewCell项

  • 如果按照上述说明操作,您应该能够使用上面的选项2分配单元格,然后可以在ScrollingTableViewCell.h/.m中设置单元格。

    我也有同样的问题。你解决了吗?你可能想尝试在这个问题上添加一个编程语言标签,这样更多的人会看到它。这是目标C吗?