0

我有 2 个用于 UITableViewCell 的类,它们显示不同的数据,但它们使用同一个表。

我正在尝试使用 bool var 分两步填充表格,但似乎我得到了一个异常,因为单元格获得了第一类分配并且不能变异......

基本上我必须单元扩展类

@interface PatientsCellNewPager : UITableViewCell { } 
@interface DoctorsCellNewPager : UITableViewCell { } 

当我在第 2 步并尝试更改单元格的类型并 [cell setData:set cellid:[NSString stringWithFormat:@"%d",indexPath.row] ]; 在 phase==2 if block 时给出异常时,代码会停止。

这是因为根据调试器的单元格仍然具有第一次初始化的类型 DoctorsCellNewPager....

不然怎么办??它必须在同一页面上使用相同的类实例,但使用多个布局并在现场定义单元格布局..这将是一个糟糕的选择

这是我的代码

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";



    if (phase==2) { //patient


        PatientsCellNewPager *cell =(PatientsCellNewPager *)  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[PatientsCellNewPager alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }







        PatientsSet *set = (PatientsSet *)[patients_set objectAtIndex:indexPath.row];       

        NSLog(@"logg %d",indexPath.row);    


        [cell setData:set cellid:[NSString stringWithFormat:@"%d",indexPath.row] ]; 




           return cell;

    }
    else if (phase==1) { //doctor


        DoctorsCellNewPager *cell =(DoctorsCellNewPager *)  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[DoctorsCellNewPager alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        }





            // Set up the cell

            DoctorsSet *set = (DoctorsSet *)[doctors_set objectAtIndex:indexPath.row];      


              [cell setData:set cellid:[NSString stringWithFormat:@"%d",indexPath.row] pass:YES ];  





        return cell;


    }


    return nil;

}

这是我的错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DoctorsCellNewPager setData:cellid:]: unrecognized selector sent to instance 0x181690'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x323ef64f __exceptionPreprocess + 114
    1   libobjc.A.dylib                     0x36632c5d objc_exception_throw + 24
    2   CoreFoundation                      0x323f31bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
    3   CoreFoundation                      0x323f2649 ___forwarding___ + 508
4

2 回答 2

1

您最后错过了pass:..参数,并且您正在调用的方法[DoctorsCellNewPager setData:celled:]没有pass设置参数。所以添加pass参数,它应该运行良好。

于 2011-09-12T23:12:22.543 回答
1

这是一个非常普遍的问题,很多人都遇到过——就在最近我也不得不处理这个问题。

Matt Gallagher 在他的博客上发布了一个关于如何实现这一点的精彩教程:

UITableViewController 中的异构单元格


好吧,我刚刚知道它是如何工作的
。//这是高度实验性的,尚未经过测试:

static NSString *PatientsCellIdentifier = @"PatientsCellNewPager";
static NSString *DoctorsCellIdentifier = @"DoctorsCellNewPager";

UITableViewCell *cell;
if(patient) {
    cell = [tableView dequeueReusableCellWithIdentifier:PatientsCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:PatientsCellIdentifier] autorelease];
    }
    // Setup cell for patient -> maybe you could use -configureCell:atIndexPath:
} else if (doctor) {
    cell = [tableView dequeueReusableCellWithIdentifier:DoctorsCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:DoctorsCellIdentifier] autorelease];
    }
    // Setup cell for doctor -> maybe you could use -configureCell:atIndexPath:
}

return cell;

让我知道这是否让你更进一步..

高温高压

于 2011-09-12T23:14:25.370 回答