使用自定义UITableViewCell时,UITableView会抛出EXC_BAD_ACCESS

时间:2011-04-26 07:39:57

标签: iphone ios uitableview exc-bad-access

我正在尝试使用自定义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

任何帮助都将受到赞赏,因为我完全被难过了。

4 个答案:

答案 0 :(得分:1)

删除

[ilabel release];
[itext release];

因为它们是自动发布的对象。

答案 1 :(得分:1)

初步想法:

  1. 您应该将一个单元格出列,以便在内存中重复使用单元格(但这是在屏幕外)。请参考UITableView documentation,尤其是dequeueReusableCellWithIdentifier:
  2. 永远不会调用两个版本行[ilabel release];[itext release];,因为它们低于return cell;行:该方法在此时停止执行。
  3. 我假设您使用CustomCell类继承UITableViewCell,对吗?我觉得很奇怪,你对它的处理方式与我使用的其他UITableViewCell示例不同,你没有发布你的头文件,所以我可以确认。

答案 2 :(得分: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];
    }

此处的代码不正确,因为您无法编写

[ilabel release];
[itext release];

return cell;之后,因为那部分代码永远不会被调用!!!

答案 3 :(得分: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