问题
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