2011-04-26 53 views
1

我想使用自定義UITableViewCell構建聯繫人表,但每次嘗試加載視圖時,都會收到EXC_BAD_ACCESS錯誤。UITableView在使用自定義UITableViewCell時拋出EXC_BAD_ACCESS

我的表的代碼如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    if(indexPath.section == 0){ 
     NSString *ilabel = [[[contactDetails objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:0]; 
     NSString *itext = [[[contactDetails objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:1]; 

     CustomCell *cell= [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     // Default to no selected style and not selected 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [cell setCellData:ilabel textVal:itext]; 


     return cell; 

     [ilabel release]; 
     [itext release]; 
    } 

} 

與我的手機類:

@implementation CustomCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code. 
    } 
    return self; 
} 

- (void)setCellData:(NSString *)ilabel textVal:(NSString *)itext { 
    _label.text = ilabel; 
    _text.text = itext; 

    // Alloc and set the frame 
    _label.frame = CGRectMake(0, 0, 286, 68); 

    // Add subview 
    [self.contentView addSubview:_label]; 
} 


- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 

    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state. 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

,因爲我完全難倒任何幫助將不勝感激。

+0

你確定'NSString * itext = [[[contactDetails objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:1]; '一直工作?順便說一句,爲你的可執行文件啓用'NSZombieEnabled'並再次查看控制檯,這應該會給出一個更好的提示,指出哪裏出了問題。 – 2011-04-26 07:42:46

+0

可能是因爲contactDetails爲零? – 2011-04-26 07:43:15

+0

不要忘記接受答案... – 2011-04-26 08:33:47

回答

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    if(indexPath.section == 0){ 
     NSString *ilabel = [[[contactDetails objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:0]; 
     NSString *itext = [[[contactDetails objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:1]; 

     CustomCell *cell= [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     // Default to no selected style and not selected 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [cell setCellData:ilabel textVal:itext]; 


     return cell; 

     [ilabel release]; 
     [itext release]; 
    } 

這裏的代碼是不正確的,因爲你不能writr

[ilabel release]; 
[itext release]; 

return cell;後,因爲代碼部分將不會被調用!

+0

是的,這是正確的....和@Hannes你能告訴我你有多少部分? – 2011-04-26 07:48:04

1

刪除

[ilabel release]; 
[itext release]; 

因爲他們是自動釋放的對象。

0

這是自定義單元格例如...

.h文件中

#import <UIKit/UIKit.h> 

#define TAGS_TITLE_SIZE  20.0f 
#define TITLE_LABEL_TAG  1 
#define DATA_TIME_LABEL_TAG 5 
#define ARROW_IMAGE_TAG  6 
#define MAIN_IMAGE_TAG  7 
#define TITLE_START_POSS 34 

// Enumeration for initiakization TableView Cells 
typedef enum { 
    NONE_TABLE_CELL    = 0, 
    TAGS_TABLE_CELL_WITHOUT_SWITCH = 1, 
    TAGS_TABLE_CELL_WITH_SWITCH = 2 
}TableTypeEnumeration; 

@protocol SwitchDelegate 
- (void)valueChangeNotify:(id)sender; 
- (void)trashClickedNotify:(id)sender; 
@end 

// Class for Custom Table View Cell. 
@interface CustomTableViewCell : UITableViewCell { 
    // Title of the cell. 
    UILabel* cellTitle; 
    UISwitch* cellSwitch; 
    UIButton* cellButton; 

    id<SwitchDelegate> delegate; 
} 

@property (nonatomic, assign) id <SwitchDelegate> delegate; 

// Set the title of the cell. 
- (void) SetCellTitle: (NSString*) _cellTitle; 
- (void) SetRowIndex: (int) _rowIndex; 
- (void) SwitchOnOff: (BOOL) _switchTo; 

- (void) InitCellTitleLable; 

// Init With Style (With additional parametr TableTypeEnumeration) 
- (id)initWithStyle: (UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier tableType:(TableTypeEnumeration)tabletypeEnum; 

@end 

.m文件

#import "CustomTableViewCell.h" 

@implementation CustomTableViewCell 

@synthesize delegate; 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
//- Initializes a table-view controller to manage a table view of a given style. ----------------- ViTo Code ----// 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    return [self initWithStyle:style reuseIdentifier:reuseIdentifier tableType:NONE_TABLE_CELL]; 
} 

- (id)initWithStyle: (UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier tableType:(TableTypeEnumeration)tabletypeEnum { 
    // Get Self. 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     // Switch table View Cells 
     switch(tabletypeEnum) { 
      case TAGS_TABLE_CELL_WITH_SWITCH: { 

       // Create and initialize Title of Custom Cell. 
       cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(TITLE_START_POSS, (44 - TAGS_TITLE_SIZE)/2, 260, 21)]; 
       cellTitle.backgroundColor  = [UIColor clearColor]; 
       cellTitle.opaque    = NO; 
       cellTitle.textColor   = [UIColor blackColor]; 
       cellTitle.highlightedTextColor = [UIColor whiteColor]; 
       cellTitle.font     = [UIFont boldSystemFontOfSize:TAGS_TITLE_SIZE]; 
       cellTitle.textAlignment  = UITextAlignmentLeft; 
       // Create and Initialize Switch of Custom Cell. 
       cellSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(185 + TITLE_START_POSS, 10, 10, 10)]; 
       [cellSwitch addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; 
       // Create and Initialize Trash Button of Custom Cell. 
       cellButton = [[UIButton alloc] initWithFrame:CGRectMake(1, 7, 32, 32)]; 
       [cellButton setImage:[UIImage imageNamed:@"remove_tag.png"] forState:UIControlStateNormal]; 
       [cellButton addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown]; 

       [self.contentView addSubview:cellTitle]; 
       [self.contentView addSubview:cellSwitch]; 
       [self.contentView addSubview:cellButton]; 
       [cellTitle release]; 
       [cellSwitch release]; 
       [cellButton release]; 

       break; 
      } 
      case TAGS_TABLE_CELL_WITHOUT_SWITCH: { 
       // Create and initialize Title of Custom Cell. 
       cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, (44 - TAGS_TITLE_SIZE)/2, 260, 21)]; 
       cellTitle.backgroundColor  = [UIColor clearColor]; 
       cellTitle.opaque    = NO; 
       cellTitle.textColor   = [UIColor blackColor]; 
       cellTitle.highlightedTextColor = [UIColor whiteColor]; 
       cellTitle.font     = [UIFont boldSystemFontOfSize:TAGS_TITLE_SIZE]; 
       cellTitle.textAlignment  = UITextAlignmentLeft; 

       [self.contentView addSubview:cellTitle]; 
       [cellTitle release]; 
      } 
      default: break; 
     } 
    } 
    return self; 
} 

-(void)touchDown: (id)sender { 
    [delegate trashClickedNotify:sender]; 
} 

-(void)valueChange:(id)sender { 
    [delegate valueChangeNotify:sender]; 
} 

- (void) InitCellTitleLable { 
    cellTitle = (UILabel *)[self.contentView viewWithTag:TITLE_LABEL_TAG]; 
} 

- (void) SetRowIndex: (int) _rowIndex { 
    cellSwitch.tag = _rowIndex; 
    cellButton.tag = _rowIndex; 
} 

- (void) SwitchOnOff: (BOOL) _switchTo { 
    cellSwitch.on = _switchTo; 

} 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
//- Set title of the Cell. ----------------------------------------------------------------------- ViTo Code ----// 
- (void) SetCellTitle: (NSString*) _cellTitle { 
    cellTitle.text = _cellTitle; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 
} 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
//- Deallocates the memory occupied by the receiver. --------------------------------------------- ViTo Code ----// 
- (void)dealloc { 
    // Delloc objects. 
    //[self.cellMainImage release]; 
    //[self.cellTitle release]; 
    //[self.cellDataTime release]; 

    // Call base delloc 
    [super dealloc]; 
} 

@end 
1

最初的想法:

  1. 你應該出列單元格試圖在內存中重用單元格(但那是關屏)。請參考UITableView documentation,特別是dequeueReusableCellWithIdentifier:
  2. 將不會調用兩條版本行[ilabel release];[itext release];,因爲它們低於return cell;行:該方法在該點停止執行。
  3. 我假設你從你的CustomCell類繼承UITableViewCell,是否正確?我覺得很奇怪,你對它的處理方式與我曾經使用過的其他UITableViewCell示例的處理方式不同,並且你沒有發佈頭文件,所以我可以確認。
相關問題