【发布时间】:2015-01-30 06:32:06
【问题描述】:
我正在制作一个应用程序,我在 uitableview 单元格中使用复选框形式的按钮。我正在更改该复选框选择和取消选择状态的图像。现在我希望当我点击另一个按钮时,表格视图的所有单元格都将被选中,并且所有单元格复选框的图像都会改变?下面是我的示例代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.lblArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *tableviewidentifier = @"cell";
TablecellTableViewCell *cell= [self.tblvie dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell==nil)
{
cell = [[TablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
}
cell.lblImage.layer.cornerRadius=3.0f;
cell.lblImage.layer.borderWidth=1.0f;
cell.backgroundColor=[UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])
{
[cell.buttonCheckbox setImage:[UIImage imageNamed:@"check_box_ok.png"]
forState:UIControlStateNormal];
}
else
{
[cell.buttonCheckbox setImage:[UIImage imageNamed:@"uncheck.png"]
forState:UIControlStateNormal];
}
cell.buttonCheckbox.tag=indexPath.row;
[cell.buttonCheckbox addTarget:self action:@selector(checkButton:) forControlEvents:UIControlEventTouchUpInside];
cell.lblText.text=[self.lblArray objectAtIndex:indexPath.row];
return cell;
}
-(IBAction)checkButton:(UIButton *)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tblvie];
NSIndexPath *indexPath = [self.tblvie indexPathForRowAtPoint:buttonPosition];
if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]]) {
[self.checkimageArray removeObject:[self.lblArray objectAtIndex:indexPath.row]];
self.titleLabel.text=@"";
}
else {
[self.checkimageArray addObject:[self.lblArray objectAtIndex:indexPath.row]];
self.titleLabel.text=[self.lblArray objectAtIndex:sender.tag];
}
[self.tblvie reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
}
这是我的按钮,我在其中单击,然后 tableview 的所有单元格都将被选中,所有单元格的图像都会更改,但它不起作用
- (IBAction)selectbtns:(id)sender {
static NSString *tableviewidentifier = @"cell";
TablecellTableViewCell *cell= [self.tblvie dequeueReusableCellWithIdentifier:tableviewidentifier];
[cell.buttonCheckbox setImage:[UIImage imageNamed:@"check_box_ok.png"]
forState:UIControlStateNormal];
}
【问题讨论】:
-
ru 在此 [cell.buttonCheckbox setImage:[UIImage imageNamed:@"check_box_ok.png"] forState:UIControlStateNormal]; 行后尝试添加 [yourtableview reloaddata]
-
是的尝试过,但是“无法加载从包中带有标识符“com.xxx.whyq.checkboxwithTableview”的笔尖引用的“”图像”
-
清理并再次运行一次
-
好你最终需要的结果
-
我想在点击按钮时选择所有单元格。
标签: ios uitableview