开发者

iphone: how to add checkboxes to a uitableviewcell?

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开发者_JS百科 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;
}


 - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"CellWithSwitch"];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithSwitch"] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

cell.textLabel.text = @"Sound Effects";
return cell;

}

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];


         if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
             cell.accessoryType = UITableViewCellAccessoryNone;
         } else {
             cell.accessoryType = UITableViewCellAccessoryCheckmark;
         }

 }


I would try subclassing UITableViewCell and make each cell the target of the buttons action. That way when the button is pressed, the event will be handled by the cell object, not the table controller. If you don't like that, you could try using the tag your assigning to the button to make a distinction. To do that, make your button selector be

checkAction:(UIButton *)_btn {}

and when you add the target,

[checkButton addTarget:self action:@selector(checkAction:) forControlEvents:UIControlEventTouchUpInside];

(notice the : at the end of the @selector())

I haven't tried the second way, but I think it would work.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜