views:

82

answers:

1

Hi I am trying to create a list of options with check boxes. I am using UITableView and adding custom type buttons to the table as check boxes. My code for cellForRowAtIndexPath method is pasted below.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect a=CGRectMake(0, 0, 310, 32);
UIImageView *aImg=[[UIImageView alloc] initWithFrame:a];
UIImageView *bImg=[[UIImageView alloc] initWithFrame:a];
aImg.image=[[[Resources getResources] getImageLoader] getMainMenuUnselectedImage];
bImg.image=[[[Resources getResources] getImageLoader] getLiveScoreSmallBg];
[aImg setContentMode:UIViewContentModeScaleToFill];
[bImg setContentMode:UIViewContentModeScaleToFill];
cell.backgroundView = [[UIView alloc] initWithFrame:a];
[cell.backgroundView addSubview:aImg];
cell.selectedBackgroundView=bImg;
cell.font = [UIFont boldSystemFontOfSize:16];
cell.selectedTextColor = [UIColor blackColor];
UIImage *checked = [UIImage imageNamed:@"check_filled.png"];
UIImage *unchecked = [UIImage imageNamed:@"check_empty.png"];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(3, 3, 30, 30)];
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[button setImage: [UIImage imageNamed:@"check_filled.png"] forState: UIControlStateSelected];
[button setImage: [UIImage imageNamed:@"check_empty.png"] forState: UIControlStateNormal];
[button setEnabled:YES];
button.tag = indexPath.row;
[button setUserInteractionEnabled:YES];
[cell.contentView addSubview:button];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 3, 250, 17)];
label.font = [UIFont boldSystemFontOfSize:15]; 
label.backgroundColor = [UIColor clearColor];
label.text = [teamsList objectAtIndex:indexPath.row];
[cell.contentView addSubview:label];

    [button release];
[label release];
[aImg release];
[bImg release];

return cell;
}

Problem is that when i run the program, the screen appears but without buttons. And also when i click any row, the program crashes. Can anybody please help? Best Regards...

+1  A: 

There're some problems with your code which may be not relevant to your problem, but still...

  1. You create cell contents each time cell is reused - so if you scroll your table back and forth the reused cells will contain multiple identical subviews - that's not what you want. To fixed that you must move creating contents to the creating cell block:

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        // Create cell contents here!
        ...
    }
    ...
    
  2. You must not release your button - since you create it with convenience method button is autoreleased

    button = [[UIButton buttonWithType:UIButtonTypeCustom] 
                         initWithFrame:CGRectMake(3, 3, 30, 30)];
    
  3. This line may also cause problems:

    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    

    Here you set selector that receives no parameters. If your buttonPressed method declared is (most common way)

    - (void) buttonPressed:(id)sender;
    

    Then you must create selector as @selector(buttonPressed:) to indicate that it actually takes 1 parameter

Vladimir
Thanks alot for quick and so useful response. I have made changes by removing release command for the button and changing IBAction to void.
Aqueel
Thanks a lot for such a quick and useful response. I have made changes by removing release for button and changing IBAction to void. Code works fine now.
Aqueel
so extra release must have been that problem. IBAction is actually the same as void so it should not make any difference. However I strongly recommend to fix what I pointed at 1st comment as well - if your table is scrollable and has large data then the performance and general look may degrade eventually
Vladimir
sure i have already fixed it. :-)
Aqueel