3

我遇到了麻烦,我正在寻求帮助。

我需要在一个单元格中保存一个标识符,所以我做了一个 UITableViewCell 的子类并将标识符属性添加到它。在我的视图控制器中,我创建单元格如下:

- (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) 
4

3 回答 3

4

您应该tableView:cellForRowAtIndexPath:保持方法签名完整:它应该返回 (UITableViewCell *) 而不是您的自定义单元格类。由于您SidebarCell是 的子类UITableViewCell,因此它应该都“正常工作”。无需致电super或其中任何一项:

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

在其他方法中,演员应该像你一样工作:

SidebarCell *cell = (SidebarCell *)[tableView cellForRowAtIndexPath:indexPath];
于 2012-04-24T14:17:12.983 回答
2
UITableViewCell* tableViewCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

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

SidebarCell * cell = (SidebarCell *)tableViewCell;

然后在您的第二行中,您将单元格转换为 a SidebarCell,但您只是在对编译器撒谎并告诉它它应该是 aSidebarCell而实际上不是。当您投射某些东西时,它需要以正确的类型开始。

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

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

编辑 由于您在代码中创建单元格,您只需将前两行替换为cellForRowAtIndexPath

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

其他一切看起来都应该按原样工作。

于 2012-04-24T03:37:00.400 回答
0

就我而言,我使用了 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;
}
于 2016-08-02T04:20:23.933 回答