Tapping Is Required Twice to Uncheck Table Cell

Tapping is required twice to uncheck table cell

You should only implement didSelectRowAtIndexPath, not didDeselectRowAtIndexPath. In there, to just flip the selection status, do

if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell.accessoryType == .Checkmark {
cell!.accessoryType = .None
}else{
cell!.accessoryType = . Checkmark
}
}
if indexPath.row == 0{
//flip the day bit
day[0] = !day[0]

}
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)

UITableViewCell Accessory Type Checked on Tap & Set other unchecked

I would keep track of the data that should be checked and change the cell in tableView:didSelectRowAtIndexPath: and update which data is checked in tableView:cellForRowAtIndexPath: like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// do usual stuff here including getting the cell

// determine the data from the IndexPath.row

if (data == self.checkedData)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// determine the selected data from the IndexPath.row

if (data != self.checkedData) {
self.checkedData = data;
}

[tableView reloadData];
}

How can I detect a double tap on a certain cell in UITableView?

If you do not want to create a subclass of UITableView, use a timer with the table view's didSelectRowAtIndex:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//checking for double taps here
if(tapCount == 1 && tapTimer != nil && tappedRow == indexPath.row){
//double tap - Put your double tap code here
[tapTimer invalidate];
[self setTapTimer:nil];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Double Tap" message:@"You double-tapped the row" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
else if(tapCount == 0){
//This is the first tap. If there is no tap till tapTimer is fired, it is a single tap
tapCount = tapCount + 1;
tappedRow = indexPath.row;
[self setTapTimer:[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(tapTimerFired:) userInfo:nil repeats:NO]];
}
else if(tappedRow != indexPath.row){
//tap on new row
tapCount = 0;
if(tapTimer != nil){
[tapTimer invalidate];
[self setTapTimer:nil];
}
}
}

- (void)tapTimerFired:(NSTimer *)aTimer{
//timer fired, there was a single tap on indexPath.row = tappedRow
if(tapTimer != nil){
tapCount = 0;
tappedRow = -1;
}
}

HTH

Checkbox in Static Cell

Put this code to cellForRowAtIndexPath

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID")
if flagCell == false {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
}

And reload tableAfter taping

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
flagCell = !flagCell
tableView.reloadData()
}

Or you can use another method for cell updating:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
flagCell = !flagCell
tableView.reloadRows(at: [indexPath], with: .automatic)
}

UITableView Multiple Selection

In your implementation of -tableView:didSelectRowAtIndexPath: you would set the table view cell's accessoryType property depending on its current value (so it would toggle on and off with multiple taps). For example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];

if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}

You could either maintain an array of selected states in addition to the cells' own accessory type state, or iterate over the cells in the table view querying for each one's state in order to read out the selected rows.

UITableView didSelectRowAtIndexPath: not being called on first tap

Any chance you accidentally typed didDeselectRowAtIndexPath?

Uncheck UITableview custom checkbox option in Swift

Instead of let cell = tableView.dequeueReusableCellWithIdentifier... I would use

let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCellVC

This way you get a reference to the cell you want to change the image in. tableView.dequeueReusableCellWithIdentifier is meant to be used in delegate method tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) when you want to recycle your cells for better performance.

Also cell.ivRiskCellImage.image!.isEqual(UIImage(named: "UncheckedBox")) doesn't work, because UIImage(named: "UncheckedBox") creates a new UIImage, which isn't the same as the UIImage you want to check it against.



Related Topics



Leave a reply



Submit