繁体   English   中英

cellForRowAtIndexPath 返回自定义单元格?

[英]cellForRowAtIndexPath returning custom cell?

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

如果您创建自定义 UITableViewCell(在本例中为 PlanetTableViewCell)是否可以通过返回方法(UITableViewCell *)返回 object,或者我应该做些什么?

如果您创建自定义 UITableViewCell(在本例中为 PlanetTableViewCell)是否可以通过返回方法(UITableView *)返回 object,或者我应该做些什么?

你可能的意思是:

通过返回 (UITableViewCell*) 的方法返回 object,

如果是这样,那么这是完全合法和合理的。

事实上,您的PlanetTableViewCell是从UITableViewCell派生的, PlanetTableViewCell所有实例也是UITableViewCell类型(在 OOP 中是一种关系)。

使用自定义单元格

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

是的,这是返回单元格的正确方法。

但是您还应该检查您的“出队”是否返回有效的单元格 object。 如果没有,您将需要创建一个。

此方法也是您应该使用标题、附件等配置单元格的地方。

示例代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM