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

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

基本上我必须单元扩展课程

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

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

这是因为根据调试器的单元格仍然具有第一次初始化时的 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
有帮助吗?

解决方案

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

马特·加拉格尔 (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;

让我知道这是否给您带来了进一步的帮助。

华泰

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top