3

トラブルに遭遇したので、助けを求めています。

識別子をセルに保存する必要があるため、UITableViewCell のサブクラスを作成し、識別子プロパティを追加しました。私のView 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;
}

問題は、セルの 1 つを選択すると、メソッド cellForRowAtIndexPath: が SidebarCell ではなく UTableViewCell オブジェクトを返すため、identifier プロパティにアクセスできないことです。

- (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 *) を返す必要があります。yourSidebarCellは のサブクラスであるため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];

上記のコード行はUITableViewCell、 ではなく を返す必要がありますSidebarCell

SidebarCell * cell = (SidebarCell *)tableViewCell;

次に、2行目でセルを a にキャストしSidebarCellますが、コンパイラに嘘をついて、実際にはそうではないのに a であるべきだと言っているだけですSidebarCell。何かをキャストするときは、正しいタイプとして開始する必要があります。

これらの 2 行の代わりに、実際の SidebarCell を作成する必要があります。これを行う方法は、それをどのように定義したかによって異なります。

それがストーリーボード、xib、またはコードで作成されているかどうかを教えてください。必要に応じて例を提供できます。

EDIT コードでセルを作成しているので、最初の 2 行を次のように置き換えるだけです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 に答える