UITableView with 3x3 grid button subviews
Im really struggling to get this to work efficiently. Basically I need a 3x3 scrolling grid of buttons. I need to know which of these buttons is pressed and so I do the following when I create a new cell
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(20, 20, 80, 80);
button1.opaque = NO;
button1.showsTouchWhenHighlighted = NO;
[button1 addTarget:self action:@selector(buttonClick:)
forControlEvents:UIControlEventTouchUpInside];
button1.tag = 0;
[foodCell.contentView addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(120, 20, 80, 80);
button2.opaque = NO;
button2.showsTouchWhenHighlighted = NO;
[button2 addTarget:self action:@selector(buttonClick:)
forControlEvents:UIControlEventTouchUpInside];
button2.tag = 1;
[foodCell.contentView addSubview:button2];
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(220, 20, 80, 80);
button3.opaque = NO;
button3.showsTouchWhenHighlighted = NO;
[button3 addTarget:self action:@selector(buttonClick:)
forControlEvents:UIControlEventTouchUpInside];
button3.tag = 2;
[foodCell.contentView addSubview:button3];`
Now when a button is pressed I get the cell using superviews and then do a bit of math using the tags to figure out which index in my array I need to look at. This seems to work but I also need to display a checkmark over buttons which have been pressed, kind of like a shopping list where once an item is pressed it would be ticked off and could also be un-ticked. I am struggling to find an efficient way of acheiving all of this.... Does anyone have any thou开发者_如何学Goghts, I would really appreciate anything as Ive been stuck for a while now.
Many thanks
Jules
Can't you just get the instance of the clicked button from the sender
of your action buttonClick:
and then add a view resembling your check-mark over that button?
精彩评论