使用扩展类进行自定义时 UITableViewCell 出现问题

发布于 2024-12-04 09:06:39 字数 2575 浏览 3 评论 0原文

我有两个 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

I have 2 classes for a UITableViewCell that show different data but they use the same table.

I'm trying to populate the table in 2 steps by using a bool var, but it seems that I get an exception because the cell gains the first class assignment and can't mutate...

basicaly I have to cell extended classes

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

the code brakes when I'm at step 2 and trying to change the type of the cell and gives an exception at [cell setData:set cellid:[NSString stringWithFormat:@"%d",indexPath.row] ];
on the phase==2 if block..

this is because cell according to the debugger still has the type DoctorsCellNewPager from the first initialization....

How can I do it otherwise??
It has to be on the same page using the same instance of the class but using multiple layouts and defining the cell layout the spot ..would be a terrible option

here's my code

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

}

here's my error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送你一个梦 2024-12-11 09:06:39

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

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

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

让我知道这是否会给您带来任何进一步的帮助..

HTH

That's a very common issue and many people came across that - Just recently I had to deal with that issue, too.

Matt Gallagher posted a great tutorial on how to accomplish that on his blog:

Heterogeneous cells in a UITableViewController


Well, I just got an idea on how it could work..
// THIS IS HIGHLY EXPERIMENTAL AND HAS NOT BEEN TESTED:

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;

Let me know if this brought you any further..

HTH

夏の忆 2024-12-11 09:06:39

您错过了最后的 pass:.. 参数,并且您正在调用的方法 [DoctorsCellNewPager setData:celled:] 没有参数 <代码>通行证设置。因此添加 pass 参数,它应该可以正常运行。

You have missed out the pass:.. param on the end, and the method you are calling, [DoctorsCellNewPager setData:celled:], doesn't have the parameter pass set. So add the pass parameter and it should run fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文