开发者

cellForRowAtIndexPath returning custom cell?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRo开发者_如何学运维wAtIndexPath:(NSIndexPath *)indexPath {
    // ...
    PlanetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlanetCell_ID"];
    return cell;
}

If your creating a custom UITableViewCell (in this case PlanetTableViewCell) is it acceptable to return that object via a method returning (UITableViewCell *), or is there something else I should be doing?


If your creating a custom UITableViewCell (in this case PlanetTableViewCell) is it acceptable to return that object via a method returning (UITableView *), or is there something else I should be doing?

You possibly meant:

to return that object via a method returning (UITableViewCell*),

If so, then it is perfectly legal and reasonable.

Indeed, your PlanetTableViewCell being derived from UITableViewCell, all instances of PlanetTableViewCell are also of the type UITableViewCell (is-a relationship in OOP).


Yes, this is the correct way to return the cell.

But you should also be checking to see if your "dequeue" is returning a valid cell object. If not, you'll need to create one.

This method is also where you should be configuring your cell with title, accessories, etc.

Sample code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // ...
    PlanetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlanetCell_ID"];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.titleLabel.text = @"Cell Title";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


Using custom cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        simpleTableIdentifier = @"dashboard_logintimeCell_ipad";
    }
    else
    {
        simpleTableIdentifier = @"dashboard_logintimeCell";
    }
    dashboard_logintimeCell *cell = (dashboard_logintimeCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib =[[NSBundle mainBundle]loadNibNamed:simpleTableIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
/*here you cell object get
like
cell.lable.text=@"yourlabeltext";
*/

    cell.backgroundColor=[UIColor clearColor];
    return cell;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜