xcode 如何使用 UITableViewCellAccessoryCheckmark 取消选中所有行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5541328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:44:02  来源:igfitidea点击:

How to uncheck all rows using UITableViewCellAccessoryCheckmark

iphonexcodecheckmark

提问by Lauren Quantrell

I've got a UITableViewwith each row containing a checkbox using UITableViewCellAccessoryCheckmark. I can't figure out how to uncheck all the checkboxes using the didSelectRowAtIndexPathmethod.

我有一个UITableView每行包含一个复选框,使用UITableViewCellAccessoryCheckmark. 我不知道如何使用该didSelectRowAtIndexPath方法取消选中所有复选框。

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

    UITableViewCell *oldCell;

    int count = [self.myTableRowNamesArray count];

    for (NSUInteger i = 0; i < count; ++i) {                                
        // Uncheck all checkboxes
        // OF COURSE THIS DOESN'T WORK
        // BECAUSE i IS AN INTEGER AND INDEXPATH IS A POINTER
        FOO: oldCell = [myTableView cellForRowAtIndexPath:(int)i];
        // GOOD CODE:
        oldCell = [penanceOptionsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

采纳答案by saadnib

Yes, cellForRowAtIndexPath:uses NSIndexPathinstead of integer so make indexpath by using

是的,cellForRowAtIndexPath:使用NSIndexPath而不是整数,因此使用

indexPathForRow:inSection:

if you are using one section then your loop is fine just pass i in row and 0 for section.

如果您使用的是一个部分,那么您的循环就可以了,只需在行中传递 i 并为部分传递 0。

回答by kennytm

Instead of modifying the .accessoryTypeof all cells in didSelectRowAtIndexPath:, I suggest storing the selected indexin some ivar, and change the .accessoryTypein the data source's -tableView:cellForRowAtIndexPath:method, i.e.

我建议将选定的索引存储在某个 ivar 中,而不是修改.accessoryType中所有单元格的,并更改数据源的方法中的 ,即didSelectRowAtIndexPath:.accessoryType-tableView:cellForRowAtIndexPath:

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

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
   ...
   cell.accessoryType = [indexPath compare:self.selectedIndexPath] == NSOrderedSame
                          ? UITableViewCellAccessoryCheckmark
                          : UITableViewCellAccessoryNone;
   ...
}

With this, only visible cells will be affected, and the million other cells outside of the screen won't need to be modified.

这样,只有可见单元格会受到影响,屏幕外的其他百万个单元格将不需要修改。



Quite right, here's a full implementation in Swift in the general case of selecting a cell .. you'd use selectedIndexPath elsewhere in the class as you see fit. For example, in cellForRowAtIndexPathto choose the appropriate cell prototype.

非常正确,这是在选择单元格的一般情况下在 Swift 中的完整实现..您可以根据需要在类中的其他地方使用 selectedIndexPath 。例如,在cellForRowAtIndexPath选择合适的单元原型。

//  SelectingTableViewController

import UIKit

class SelectingTableViewController: UITableViewController   
    {
    internal var selectedIndexPath:NSIndexPath? = nil

    override func viewDidLoad()
        {
        super.viewDidLoad()
        tableView.estimatedRowHeight = 68.0
        tableView.rowHeight = UITableViewAutomaticDimension

        self.clearsSelectionOnViewWillAppear = false;
        }

    override func tableView
        (tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath)
            {
            print("did select....")

            // in fact, was this very row selected,
            // and the user is clicking to deselect it...
            // if you don't want "click a selected row to deselect"
            // then on't include this clause.
            if selectedIndexPath == indexPath
                {
                print("(user clicked on selected to deselect)")
                selectedIndexPath = nil
                tableView.reloadRowsAtIndexPaths(
                    [indexPath],
                    withRowAnimation:UITableViewRowAnimation.None)

                tableView.deselectRowAtIndexPath(indexPath, animated:false)
                return
                }

            // in fact, was some other row selected??
            // user is changing to this row? if so, also deselect that row
            if selectedIndexPath != nil
                {
                let pleaseRedrawMe = selectedIndexPath!
                // (note that it will be drawn un-selected
                // since we're chaging the 'selectedIndexPath' global)
                selectedIndexPath = indexPath
                tableView.reloadRowsAtIndexPaths(
                    [pleaseRedrawMe, indexPath],
                    withRowAnimation:UITableViewRowAnimation.None)
                return;
                }

            // no previous selection.
            // simply select that new one the user just touched.
            // note that you can not use Apple's willDeselectRowAtIndexPath
            // functions ... because they are freaky
            selectedIndexPath = indexPath
            tableView.reloadRowsAtIndexPaths(
                [indexPath],
                withRowAnimation:UITableViewRowAnimation.None)

            }

    }

回答by Ole Begemann

for (UITableViewCell *cell in [myTableView visibleCells]) {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

But really, you'd be better off just modifying the one cell that actually has the checkmark set. You have to have stored this information somewhere in your model anyway.

但实际上,您最好只修改实际设置了复选标记的一个单元格。无论如何,您必须将此信息存储在模型中的某处。

回答by Tieme

You're probably setting some kind of property with this method. So what i do is:

您可能正在使用此方法设置某种属性。所以我要做的是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1. first unsetting the property
    [object someProperty:nil];

    // 2. call the reloadData method to uncheck all the checkmarks
    [tableView reloadData];

    // 3. check the selected cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

    // 4. set the checked property
    [object setSomeProperty:[indexpath row]];
}

And in my cellForRowAtIndexPath methods i got something like the following code:

在我的 cellForRowAtIndexPath 方法中,我得到了类似以下代码的内容:

    if([object someProperty] == [indexpath row]){
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];        
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }