来自Nib UITableView的自定义单元格

Lee*_*ong 1 iphone uitableview

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

Jef*_*ley 7

你需要做两件事:

  1. 为要从笔尖使用的单元格创建单独的单元格标识符.使用"any-cell"将导致您的单元格被重用于不同的行.
  2. initWithFrame:reuseIdentifier:您需要从笔尖初始化单元格,而不是UITableViewCell的方法.查看Apple的Table View编程指南中有关使用nib的部分.

此外,虽然这在技术上并不是必需的,但UITableViewCell的initWithFrame:reuseIdentifier:方法已被弃用了initWithStyle:reuseIdentifier:.