开发者

UITableView throws EXC_BAD_ACCESS when using custom UITableViewCell

I am trying to build a contact table using custom UITableViewCell, but every time I try load the view, I get an EXC_BAD_ACCESS error.

The code for my Table is as follows:

    - (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
    开发者_如何学Python    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setCellData:ilabel textVal:itext];


        return cell;

        [ilabel release];
        [itext release];
    }

}

with my cell class:

@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

Any help will be appreciated as I am completely stumped.


Remove

[ilabel release];
[itext release];

Because they are auto released Objects.


Initial thoughts:

  1. You should dequeue a cell in an attempt to reuse cell's in memory (but that are off screen). Please reference the UITableView documentation, particularly dequeueReusableCellWithIdentifier:.
  2. The two release lines, [ilabel release]; and [itext release]; will never be called because they are below the return cell; line: the method ceases executing at that point.
  3. I am assuming you are inheriting from UITableViewCell with your CustomCell class, correct? I find it odd that you are treating it differently than other UITableViewCell examples I have worked with and you didn't post your header file so I could confirm.


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

The code here is not correct because you cant writr

[ilabel release];
[itext release];

after return cell;, because that part of code will never called !!!


Here is custom cell example ...

.h file

#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 file

#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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜