2012-04-24 9 views
3

問題が発生しました。ヘルプが必要です。cellForRowAtIndexPathからカスタムセルを返します。

セルに識別子を保存する必要があるため、サブタイトル の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がという問題がある:代わりにSidebarCellの UTableViewCellオブジェクトを返しますので、私はドン」私の見解 コントローラでは、私は次のように細胞を作成します識別子プロパティにアクセスできます。

- (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]; 
    }  
} 

サイドバーセルオブジェクトを返す方法はありますか?

ありがとうございます。

編集

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

+0

こんにちは@ダニエルビア、ありがとう、ありがとう。私は今家にいるので、貼り付けることはできませんが、識別子プロパティがUITableViewCellに存在しないということを覚えています。 – Tae

答えて

4

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:@"[email protected]"]; 
    } 
    // ... set up the cell here ... 
    return cell; 
} 

、キャストはあなたがそれを持っていると同じように動作するはずです:私の場合は

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

私のエラーは、SidebarCellの代わりにUITableViewCellを返しています。 – Tae

+0

ああ、グレッグに感謝します。これはまさに私が似たような状況のために入力する必要があったものです。 –

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

上記のコード行はUITableViewCellであり、SidebarCellではありません。

SidebarCell * cell = (SidebarCell *)tableViewCell; 

は、その後、あなたの二行目に、あなたはSidebarCellに、細胞をキャストしていますが、単にコンパイラに横たわって、それが実際にいないとき、それはSidebarCellでなければならないことを言っています。あなたが何かをキャストするとき、それは正しいタイプとして始める必要があります。

2行の代わりに、実際のサイドバーセルを作成する必要があります。どのようにこれを行うかは、それをどのように定義したかによって異なります。

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

EDIT あなたは、コード内のセルを作成しているので、あなたは単にこれにcellForRowAtIndexPathに最初の2行を交換する必要があります。それはそのまま動作するはずのような他の

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

すべてが見えます。

+0

こんにちは@lnafziger、私はちょうどコードを公開します。私は何らかの形でnibファイルで作業しているわけではなく、ちょうどコードです。あなたが私を例にして助けてくれるなら、私は感謝します。 – Tae

+0

Greg Hoのコメントは、OPが探しているものを達成するためのより簡潔な方法です:SidebarCell * cell =(SidebarCell *)[tableView cellForRowAtIndexPath:indexPath]; –

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; 
} 
関連する問題