我有一个UITableViewController
,UITableView
它的单元格通过accessoryView
. 这些按钮可以selected/deselected
在用户点击它们时出现。它table
本身由NSMutableArray
包含多个NSString
参数的对象组成,“名称”参数显示在表中。
我现在要做的是在选择按钮时,将OBJECT
链接到相应"name"
参数的相应链接添加到NSMutableArray
. 用户没有选择单元格,而是单击了附加到单元格的按钮。
这是我在每个中selects/deselects
的方法:button
cell
-(void)buttonTouched:(id)sender
{
UIButton *btn = (UIButton *)sender;
if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"CheckBox1.png"]])
{
[btn setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
// statement to add object to NSMutableArray
}
else
{
[btn setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateNormal];
// statement to remove object from NSMutableArray
}
}
//Here is my code that shows how I initialize my button and cell in my UITableView
- (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];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(280, 57, 25, 25)];
[testButton setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateSelected];
[testButton setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
[testButton setUserInteractionEnabled:YES];
[testButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[testButton setTag:cell.tag];
[cell setAccessoryView:testButton];
//[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
// Configure the cell...
TestObject *tObject = [[DataModel sharedInstance].testList objectAtIndex:indexPath.row];
cell.textLabel.text = tObject.testTitle;
return cell;
}
我的问题不是将对象实际添加到数组中,而是从按钮派生相应的单元格,然后从该单元格派生正确的对象。