开发者

How to add UIScrollView just in the first UITableCellView?

开发者 https://www.devze.com 2023-01-19 20:50 出处:网络
i have a uitableview in the uiviewcontroller, i made a scrollview in the viewload event. i am adding it to tableview\'s first cell. but i scroll the tableview it displays more than one scrollview afte

i have a uitableview in the uiviewcontroller, i made a scrollview in the viewload event. i am adding it to tableview's first cell. but i scroll the tableview it displays more than one scrollview after 5 cell passed.

here is the code.

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

    static NSString *CellIdentifier = @"customCell";

    DetailCellViewController *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nibObjects =[[NSBundle mainBundle] loadNibNamed:@"DetailCell开发者_运维知识库View" 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;
}

in which event should i add & remove scrollview?


My guess is that you're getting a dequeued UITableViewCell that already contains the UIScrollView. If you really care about separating cell types, I'd recommend setting it up so that you at least have two CellIdentifier strings. (There are times where I've set up a UITableView to handle 4+ different cell types; once you go beyond one cell type, it's pretty much just more of the same.)


My suggested solution: (see explanation below code)


- (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;
}

NOTE: I HIGHLY recommend using Option 2 above. By far, the best results I've found when using custom/non-standard UITableViewCells is to make my own UITableViewCell subclass and xib to go along with it. Here are the steps for that:

  1. Create a subclass of UITableViewCell (we'll call yours ScrollingTableViewCell.h/.m)
  2. Class forward/import ScrollingTableViewCell into your UITableViewController (or UIViewController that's hosting a UITableView).
  3. Create a class property of type ScrollingTableViewCell (we'll call yours ScrollingCell).
  4. Create a View (New File > User Interface > View) (we'll call yours ScrollingTableViewCell.xib).
  5. Delete the stock view item in the xib and replace it with a UITableViewCell item.

    Alternative #4/5
  6. Create an empty Xib.
  7. Add a UITableViewCell item.

    VERY IMPORTANT
  8. In the xib, the File's Owner is the ViewController, NOT the UITableViewCell. The cell's class is ScrollingTableViewCell.
  9. In IB, connect the ViewController's ScrollingCell property to the UITableViewCell item.

If you follow the above instructions, you should be able to allocate your cell using Option 2 above and then you can set up your cell in ScrollingTableViewCell.h/.m.

0

精彩评论

暂无评论...
验证码 换一张
取 消