如何实现自定义复选标记按钮取决于数组计数,并且需要在这些按钮中允许多选。目前我是这样使用的,
int YAxis = 300;
for (int x=0; x< [myResultArray count];x++)
{
checkmarkButton = [UIButton buttonWithType:UIButtonTypeCustom];
checkmarkButton.frame = CGRectMake(100, YAxis, 20, 20);
checkmarkButton.tag = selectedServiceTag;
[self.view addSubview:checkmarkButton];YAxis = YAxis+30;
}
设置复选标记的图像:
[checkmarkButton setBackgroundImage:[UIImage imageNamed:@"checkBox_none"] forState:UIControlStateNormal];
[checkmarkButton addTarget:self action:@selector(serviceSelected:) forControlEvents:UIControlEventTouchUpInside];
然后我实现了动作方法。
-(void)serviceSelected
{
checkmarkButton.selected = !checkmarkButton.selected;
if (checkmarkButton.selected)
{
[checkmarkButton setBackgroundImage:[UIImage imageNamed:@"checkBox"] forState:UIControlStateNormal];
}
else
{
[checkmarkButton setBackgroundImage:[UIImage imageNamed:@"checkBox_none"] forState:UIControlStateNormal];
}
}
但是,在选择时只选择最后一个索引按钮。