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.
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.
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;
}
}
}
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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With