Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 520
subodhbahl Avatar asked Nov 06 '11 18:11

subodhbahl


People also ask

What is prototype cell?

A prototype cell acts a template for your cell's appearance. It includes the views you want to display and their arrangement within the content area of the cell. At runtime, the table's data source object creates actual cells from the prototypes and configures them with your app's data.


2 Answers

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;
        }
    }
}
like image 90
chown Avatar answered Sep 28 '22 15:09

chown


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
        }
    }
}
like image 45
Stefan Grandjean-Thomsen Avatar answered Sep 28 '22 15:09

Stefan Grandjean-Thomsen