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

时间:2010-10-09 19:22:32

标签: uitableview uiscrollview

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

这是代码。

- (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?

1 个答案:

答案 0 :(得分:2)

我的猜测是你得到一个已经包含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;
}

注意:我 HIGHLY 建议使用上面的选项2。到目前为止,我在使用自定义/非标准UITableViewCells时发现的最好结果是使我自己的UITableViewCell子类和xib同时使用它。以下是步骤:

  1. 创建UITableViewCell的子类(我们将其称为ScrollingTableViewCell.h / .m)
  2. 将ScrollingTableViewCell转发/导入您的UITableViewController(或托管UITableView的UIViewController)。
  3. 创建ScrollingTableViewCell类型的类属性(我们称之为ScrollingCell)。
  4. 创建视图(新文件>用户界面>视图)(我们将您的名称称为ScrollingTableViewCell.xib)。
  5. 删除xib中的库存视图项,并将其替换为UITableViewCell项。

    <替代#4/5
  6. 创建一个空Xib。
  7. 添加UITableViewCell项目。

    非常重要
  8. 在xib中,File的Owner是ViewController, NOT UITableViewCell。单元格的 是ScrollingTableViewCell。
  9. 在IB中,将ViewController的ScrollingCell属性连接到UITableViewCell项。
  10. 如果您按照上述说明操作,您应该能够使用上面的选项2分配您的单元格,然后您可以在ScrollingTableViewCell.h / .m中设置您的单元格。

相关问题