开发者

how to define uitableviewcells

I am creating a uitableview that has two sections, section == 0 has 5 rows, and section == 1 has 1 row.

I have also declared several functions in an objective-c class that I would like to hook into each of the five rows. However I am not sure how to implement this.

I am thinking it is something like

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell开发者_如何学Go == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

//---------- In here put a bunch of IF statments, declaring each cell? and linking it to
//---------- the function I have declared in the objective-c class

    return cell;
}


Usually the cells are setup in tableView:cellForRowAtIndexPath:. Determining the function that's called when a cell is tapped is usually done in tableView:didSelectRowAtIndexPath.

When setting up the cell in tableView:cellForRowAtIndexPath: you can specify the section and row using indexPath like this:

if(indexPath.section == 0){

    if(indexPath.row == 0){

        // setup this cell...

    }else if(indexPath.row == 1){

    }// ...

}else if(indexPath.section == 1){

    if(indexPath.row == 0){

    }else if(indexPath.row == 1){

    }// ...
}

Do something similar in tableView:didSelectRowAtIndexPath when determining which function is called when the user taps each cell. The relevant documentation from Apple can be found here.


Typically this function would be configuring the cell by pulling data out of an array, database, or some other indexed collection:

if ([indexPath section] == 0) {
    [[cell textLabel] setText:[sectionOneValues objectAtIndex:[indexPath row]]];
}
else if ([indexPath section] == 0) {
    [[cell textLabel] setText:[sectionTwoValues objectAtIndex:[indexPath row]]];
}

In your case it sounds like you want to "hook into" some other functions for the 5 rows in section 0, so you'd be doing something other than using a sectionOnValues array in the if clause. It's a little hard to be more specific without knowing what you mean to do with these functions that hook in.


Depends, are all the cells equal, but only changing their content? In that case, you would be right.

On the other hand, if you plan on adding more Labels, images, or whatnot into the cell, then, for each different row, you would have to tweak the way the cells are being created, therefore, having to add new CellIdentifiers to avoid mixing cells, and changing the code inside the if (cell == nil) {} to configure each cell design.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜