您的位置:首页 > 产品设计 > UI/UE

xib自定义uitablecell 的两种写法(uitableview custom cell from xib)

2012-12-08 16:42 387 查看
Method #1:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HFCustomCell"];

    if (cell == nil) {

        // Create a temporary UIViewController to instantiate the custom cell.

        UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"HFCustomCell" bundle:nil];

        // Grab a pointer to the custom cell.

        cell = (BDCustomCell *)temporaryController.view;

        [[cell retain] autorelease];

        // Release the temporary UIViewController.

        [temporaryController release];

    }

    return cell;

}

Method #2:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HFCustomCell"];

    if (cell == nil) {

        // Load the top-level objects from the custom cell XIB.

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HFCustomCell" owner:self options:nil];

        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).

        cell = [topLevelObjects objectAtIndex:0];

    }

    return cell;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: