How to reset the tableview cells accessorytype to none on a click of a button?

﹥>﹥吖頭↗ 提交于 2019-12-23 09:59:36

问题


I am trying to code a reset button, so that when I click on that button all the tableview cells' accessory type get set to none.

I have a clear concept of how to do it, but I am pretty new to iPhone development so I just need help with what methods to call.

The steps I think I need to take: I am iterating through all the rows using a for loop - so I am counting the number of cells (successfully done). My problem is, I have no clue how to check for each of those rows/cells if the accessory type is CheckMark and set it to none.

Alternatively, I can set all of my cells to AccessoryNone, but I am already doing some calculations inside the:

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

So I am not sure how can I achieve this.


回答1:


No need to check what the accessoryType is first, just assign UITableViewCellAccessoryNone to all of them. This should work for what you are trying to do:

// replace clickedResetButton with your action handler method for that button
- (IBAction)clickedResetButton:(id)sender {
    for (int section = 0, sectionCount = self.tableView.numberOfSections; section < sectionCount; ++section) {
        for (int row = 0, rowCount = [self.tableView numberOfRowsInSection:section]; row < rowCount; ++row) {
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.accessoryView = nill;
        }
    }
}



回答2:


Swift 3.1

func resetAccessoryType(){
    for section in 0..<self.tableView.numberOfSections{
        for row in 0..<self.tableView.numberOfRows(inSection: section){
            let cell = self.tableView.cellForRow(at: IndexPath(row: row, section: section))
            cell?.accessoryType = .none
        }
    }
}


来源:https://stackoverflow.com/questions/8029291/how-to-reset-the-tableview-cells-accessorytype-to-none-on-a-click-of-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!