【问题标题】:how to set accessoryType when cell has been selected选择单元格时如何设置附件类型
【发布时间】:2011-09-08 03:59:16
【问题描述】:

我一直在试图弄清楚如何在选择单元格时将附件类型设置为 UITableViewCellAccessoryCheckmark,但我找不到合适的示例。

如果您知道如何执行此操作或有好的教程,请告诉我,那将是很棒的。

【问题讨论】:

  • Here 是 UITableView 和 UITableViewCell 的一个非常好的示例。

标签: iphone ios accessorytype


【解决方案1】:

要将用户限制在一个选项中,即创建一个仅包含一个选项的排他列表,您可以按照以下步骤操作;

首先,在您的 .h 文件中声明一个全局索引路径以跟踪已选择的单元格 ->

NSIndexPath *oldIndexPath;

在创建单元格时,一定要将附件类型设置为none,这样在看到表格时默认不选中任何单元格;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CallIdentifier"];
        cell.accessoryType = UITableViewCellAccessoryNone;      
    }
    return cell; 
}

最后,在 didSelectRowAtIndexPath 委托方法中,添加以下代码,该代码将从已选择的单元格中删除复选标记,并为新选择的单元格添加复选标记。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (oldIndexPath==nil) { // No selection made yet
        oldIndexPath=indexPath;
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    else {
        UITableViewCell *formerSelectedcell = [tableView cellForRowAtIndexPath:oldIndexPath]; // finding the already selected cell
        [formerSelectedcell setAccessoryType:UITableViewCellAccessoryNone];

        [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; // 'select' the new cell
        oldIndexPath=indexPath;
    }   
}

希望一切顺利! :)

【讨论】:

    【解决方案2】:

    这样的事情可能会奏效:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    

    要回答下面的评论,只需像这样以相同的方法推送一个 viewController:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    
        // Then push a new view
        iPhoneCustomViewController *myVC = [[iPhoneCustomViewController alloc] initWithNibName:@"iPhoneCustomViewController" bundle:nil];
        [self.navigationController pushViewController:myVC animated:YES];
        [myVC release];
    
        // And deselect the row (if desired)
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    【讨论】:

    • 这个效果很好,我将 [self tableview:mytab...我会推送新视图还是有更好的处理方式?
    • 是的,只需在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 方法中推送一个新视图。我不认为你可以有多个选择,除非表格允许编辑并且 table.isEditing 是真的。
    【解决方案3】:

    你知道吗:

    1.) UITableView 跟踪已选择行的索引路径?它在一个名为 indexPathsForSelectedRows 的数组中

    2.) UITableView 有一个标志,您可以设置它以使其成为单选或多选。您可以通过调用设置器 setAllowsMultipleSelection:(BOOL) 来更改它。

    所以,假设表格已经设置为单选,我们可以在 tableView:CellForRowAtIndexPath 方法中进行如下操作...

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *simpleTableIdentifier = @"CellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [[cell textLabel] setText:@"Some Text"];
    
    NSArray *selectedIndexPaths = [tableView indexPathsForSelectedRows];
    
    if ([selectedIndexPaths containsObject:indexPath]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    
    return cell;}
    

    CellForRowAtIndexPath 的此实现将在选择单元格时为您提供一个没有灰色背景的干净复选标记。您需要在 didSelectRowAtIndexPath 委托方法中设置复选标记,以确保单元格在被选中时获得复选标记。

    无需创建单独的 ivars 或其他任何东西来跟踪已选择或未选择的内容。正如 Apple 所期望的那样,它们都整齐地包含在 UITableView 中。

    【讨论】:

      【解决方案4】:
      UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
       newCell.accessoryType=UITableViewCellAccessoryCheckmark;
      

      在 didSelectRowAtIndexPath 委托方法中实现这个

      【讨论】:

        【解决方案5】:

        来自docs

        委托处理此方法中的选择。它的一件事 可以做的是专门分配复选标记图像 (UITableViewCellAccessoryCheckmark) 到一节中的一行 (广播列表样式)。编辑属性时不调用该方法 表的设置为YES(即表视图正在编辑中 模式)。请参阅表视图编程指南中的“管理选择”以了解 iOS 以获取与此相关的更多信息(和代码示例) 方法。

        这是一个例子:

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
           UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]
           cell.UITableViewAccessoryType = UITableViewCellAccessoryCheckmark;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-02
          • 2022-10-16
          • 2019-11-12
          相关资源
          最近更新 更多