views:

14

answers:

1

Hi, I just can't seem figure out how to do this hopefully someone here can help me. In my tableview i want to add checkboxes to each cell and if you touch the checkbox a checkmark appears and a specific action is supposed to happen. but if you just touch the cell a different action is supposed to happen. Does anyone have an idea how to do this?? I started doing the following. But i think my approach creates memory leaks and i don't know how to perform an action only for the button that was pressed instead for all of them..... It would be great if someone could help me out....

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

}
NSArray *tempArray = [[exerciseNames objectAtIndex:indexPath.section] objectForKey:@"rowValues"];

UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
checkButton.tag = indexPath.row;
[checkButton setFrame:CGRectMake(10, 10, 23, 23)];
if (checkButtonPressed == YES) {
    [checkButton setBackgroundImage:[[UIImage imageNamed:@"checked.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
}
else {
    [checkButton setBackgroundImage:[[UIImage imageNamed:@"unchecked.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
}
[checkButton addTarget:self action:@selector(checkAction) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:checkButton];

cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", [[tempArray objectAtIndex:indexPath.row] objectForKey:@"full"], [[tempArray objectAtIndex:indexPath.row] objectForKey:@"short"]];

// Set up the cell...

return cell;
}
A: 

In Cocoa Touch, a UISwitch is the UI paradigm equivalent for a checkbox on a desktop UI.

Your checkAction handler can check which control called it, and only change that one control.

hotpaw2