0

我以编程方式创建具有不同部分和行的表格,我在表格内创建复选框,并将图片作为一个框

我想为此复选框设置限制

你能帮帮我吗

编辑 :

我的问题是我怎样才能只选择一个复选框 - 选择限制

以及如何取消选择一个框并删除复选标记

这是复选框行的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
    NSArray *contents = [[self sectionContents] objectForKey:key];
    NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

    if (indexPath.section != 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.textLabel.text =contentForThisRow;
        return cell;
    }
    else {
        CheckBoxAbsenceTableViewCell *cell = (CheckBoxAbsenceTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CheckBoxAbsenceTableViewCell"];

        cell.imageView.image=[UIImage imageNamed:@"emptycheck-box.png"];
        cell.checkBox.image = image;
        if(indexPath.row == 0){
            cell.absenceCode.text =@"Red";
        }
        else if (indexPath.row == 1){
            cell.absenceCode.text =@"Green";

        }else if(indexPath.row == 2){
            cell.absenceCode.text =@"Blue";
        }
        return cell;
    }
}  

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      CheckBoxAbsenceTableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
      selectedCell.checkBox.image =[UIImage imageNamed:@"checkmark.png"];
  }

提前致谢!

4

2 回答 2

0

从 UI 设计的角度来看,您应该使用 cell.accessoryType 和 UITableViewCellAccessoryCheckmark 附件。

您需要跟踪数据模型中选定(即选中)的 indexPath。让我们称之为 self.idxPathSelected。

这 didSelectRowAtIndexPath 将为所选行添加复选标记,并从先前选择的行中清除复选标记:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 [tableView deselectRowAtIndexPath:indexPath animated:NO];
 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 cell.accessoryType = UITableViewCellAccessoryCheckmark;

 if (self.idxPathSelected != nil)
 {
  cell = [tableView cellForRowAtIndexPath:self.idxPathSelected];
  cell.accessoryType = UITableViewAccessoryNone;
 }

 self.idxPathSelected = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];

}

在你的 cellForRowAtIndexPath 中,是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...

 if ((self.idxPathSelected != nil) && ([self.idxPathSelected compare:indexPath] == NSOrderedSame))
  cell.accessoryType = UITableViewCellAccessoryCheckmark;
 else
  cell.accessoryType = UITableViewCellAccessoryNone;
...
}
于 2012-08-09T18:56:20.063 回答
0

简单的解决方案是将UIInteger _selectedIndex实例变量添加到您的视图控制器。
在 viewDidLoad 中将其设置为 -1(未选择任何行)
然后当用户选择一个单元格时,保存所选索引并重新加载 tableView:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {  
      _selectedIndex = indexPath.row;
      [self.tableView reloadData];
  }  

更改cellForRowAtIndexPath方法,使其仅选择选定的单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
    NSArray *contents = [[self sectionContents] objectForKey:key];
    NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

    if (indexPath.section != 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.textLabel.text =contentForThisRow;
        return cell;
    }
    else {
        CheckBoxAbsenceTableViewCell *cell = (CheckBoxAbsenceTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CheckBoxAbsenceTableViewCell"];

        if (indexPath.row == _selectedIndex) {
            cell.checkBox.image =[UIImage imageNamed:@"checkmark.png"];
        }
        else {
            cell.checkBox.image = [UIImage imageNamed:@"emptycheck-box.png"];;
        }


        if(indexPath.row == 0){
            cell.absenceCode.text =@"Red";
        }
        else if (indexPath.row == 1){
            cell.absenceCode.text =@"Green";

        }else if(indexPath.row == 2){
            cell.absenceCode.text =@"Blue";
        }
        return cell;
    }
} 
于 2012-08-09T20:56:41.480 回答