2012-04-24 43 views
3

我遇到了麻烦,我在寻求帮助。从cellForRowAtIndexPath返回一个自定义单元格:

我需要将一个标识符保存在一个单元格中,所以我创建了UITableViewCell的子类 并添加了标识符属性。我认为 控制器I创建细胞如下:

- (SidebarCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell* tableViewCell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; 
    SidebarCell * cell = (SidebarCell *)tableViewCell; 
    Category *category = [categoryDataController.categories objectAtIndex:indexPath.row]; 
    cell.textLabel.text = category.name; 

    if (category.checked == 1) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 
} 

的问题是,当我选择其中的一个单元,所述方法 的cellForRowAtIndexPath:返回一个UTableViewCell对象而不是SidebarCell的 ,因此我不无法访问标识符属性:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO]; 
    SidebarCell *cell = (SidebarCell *)[tableView cellForRowAtIndexPath:indexPath]; // Trying to cast from UITableViewCell to SidebarCell 

    if (cell.accessoryType == UITableViewCellAccessoryNone) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [CategoryDataController updateRow:cell.identifier status:1]; // Here the app crashes 
    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [CategoryDataController updateRow:cell.identifier status:0]; 
    }  
} 

有没有方法cellForRowAtIndexPath:返回一个Sidebarcell对象?

预先感谢。

编辑

的SidebarCell.h

#import <UIKit/UIKit.h> 

@interface SidebarCell : UITableViewCell 

@property (nonatomic) NSInteger identifier; 

@end 

SidebarCell.m:

#import "SidebarCell.h" 

@implementation SidebarCell 

@synthesize identifier; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 

错误:

2012-04-24 09:21:08.543 Dongo[2993:11603] -[UITableViewCell identifier]: unrecognized selector sent to instance 0x6d87d50 
2012-04-24 09:21:08.571 Dongo[2993:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell identifier]: unrecognized selector sent to instance 0x6d87d50' 
*** First throw call stack: 
(0x14b1052 0x1a65d0a 0x14b2ced 0x1417f00 0x1417ce2 0x116bc 0x48e71d 0x48e952 0xd1686d 0x1485966 0x1485407 0x13e87c0 0x13e7db4 0x13e7ccb 0x239d879 0x239d93e 0x3fea9b 0x2e85 0x2715 0x1) 
terminate called throwing an exception(lldb) 
+0

应用程序崩溃时出现什么错误? – danielbeard 2012-04-24 02:05:22

+0

嗨@danielbeard,谢谢你的关注。我现在在家,所以我不能粘贴它,但我记得它说,标识符属性不存在于UITableViewCell中。 – Tae 2012-04-24 02:09:45

回答

4

你应该离开tableView:cellForRowAtIndexPath:方法签名不变:它应该返回(UITableViewCell的*),而不是您的自定义单元格类。由于您SidebarCellUITableViewCell一个子类,它都应该“只是工作”。无需调用super或任何认为:

SidebarCell *cell = (SidebarCell *)[tableView cellForRowAtIndexPath:indexPath]; 
+0

我的错误是返回一个UITableViewCell而不是SidebarCell。 – Tae 2012-04-24 15:36:25

+0

啊,谢谢Greg。这正是我需要为类似情况输入的内容。 – 2014-03-05 18:45:58

2
UITableViewCell* tableViewCell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; 

上面的代码行必须返回UITableViewCell而不是SidebarCell

SidebarCell * cell = (SidebarCell *)tableViewCell; 

然后在你的第二个行你施放细胞对SidebarCell,但你只是躺在编译器,并告诉它,它应该是一个SidebarCell,当它其实不是。当你投出一些东西时,需要从正确的类型开始。

而不是这两行,你应该创建一个实际的SidebarCell。你如何做到这一点取决于你如何定义它。

让我知道它是在故事板,xib中还是在代码中创建的,如果需要,我可以提供一个示例。

编辑 由于要在代码中创建的单元格,你只需要使用它来取代在cellForRowAtIndexPath前两行:

SidebarCell * cell = [[SidebarCell alloc] init]; 

其他的一切看起来像它应该工作原样。

+0

嗨@lnafziger,我只是发布代码。我不用任何方式处理nib文件,只是代码。如果您能以示例帮助我,我将不胜感激。 – Tae 2012-04-24 12:24:22

+0

看起来好像Greg Ho的评论是一种更简洁的方法来完成OP所寻找的内容:SidebarCell * cell =(SidebarCell *)[tableView cellForRowAtIndexPath:indexPath]; – 2014-03-05 18:46:55

0

在我的情况下,我用一个类的自定义单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SidebarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; 
    if (cell == nil) { 
     cell = [[SidebarCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:@"[email protected]"]; 
    } 
    // ... set up the cell here ... 
    return cell; 
} 

在其他方法中,中投应该因为你拥有它只是工作tempTableViewcell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *identifier = @"cellidentifier"; 

    tempTableViewcell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
    } 

    cell.textLabel.text = @"abc"; 
    return cell; 
} 
相关问题