来自Nib UITableView的自定义单元格 [英] Custom Cell from Nib UITableView

查看:62
本文介绍了来自Nib UITableView的自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试各种方法,但没有成功更改以下代码...

I have been trying various methods with no success to alter the following code...

如果我将所有单元格设置为此,我有一个nib文件可以正常工作,但我只希望其中一个switch语句具有自定义单元格nib文件.有什么想法吗?

I have a nib file that works fine if I set all cells to this but I only want one of the switch statements to have the custom cell nib file. Any ideas?

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

    NSUInteger row = [indexPath row];
    NSInteger section = [indexPath section];
    UITableViewCell *cell;


    switch (section) 
    {
        case 0:

            cell = [tableView dequeueReusableCellWithIdentifier:@"any-cell"];
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
            cell.text =  [showTimes objectAtIndex:row];
            break;
        case 1:

            cell = [tableView dequeueReusableCellWithIdentifier:@"any-cell"];
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
            cell.text =  [showTimes objectAtIndex:row];
            break;
        default:
            cell = [tableView dequeueReusableCellWithIdentifier:@"any-cell"];
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
            cell.text =  [showTimes objectAtIndex:row];
            break;
    }




    return cell;

}

推荐答案

您将需要做两件事:

  1. 从笔尖中为要使用的单元格创建单独的单元格标识符.使用任意单元格"将导致您的单元格可重复用于不同的行.
  2. 您需要从笔尖初始化单元格,而不是UITableViewCell的 initWithFrame:reuseIdentifier:方法.检查 Apple的Table View编程指南中有关使用笔尖的部分.
  1. Make a separate cell identifier for the cell that you want to use from the nib. Using "any-cell" will result in your cell being reused for different rows.
  2. Instead of UITableViewCell's initWithFrame:reuseIdentifier: method, you need to initialize the cell from your nib. Check Apple's Table View Programming Guide's section on using nibs.

此外,尽管这在技术上不是必需的,但不推荐使用UITableViewCell的 initWithFrame:reuseIdentifier:方法,而推荐使用 initWithStyle:reuseIdentifier:.

Also, though this isn't technically required, UITableViewCell's initWithFrame:reuseIdentifier: method has been deprecated in favor of initWithStyle:reuseIdentifier:.

这篇关于来自Nib UITableView的自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆