2012-04-24 73 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; 
} 
相關問題