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

我需要在一个单元格中保存一个标识符,所以我做了一个子类
的 UITableViewCell 并添加到它的标识符属性。在我看来
Controller 我按如下方式创建单元格:

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

最佳答案

您应该保持 tableView:cellForRowAtIndexPath: 方法签名不变:它应该返回 (UITableViewCell *) 而不是您的自定义单元格类。由于您的 SidebarCellUITableViewCell 的子类,因此它应该都“正常工作”。无需调用 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];

关于objective-c - 从 cellForRowAtIndexPath : 返回自定义单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10290808/

10-14 23:35
查看更多