Uitableview: How to Disable Selection for Some Rows But Not Others

UITableview: How to Disable Selection for Some Rows but Not Others

You just have to put this code into cellForRowAtIndexPath

To disable the cell's selection property: (while tapping the cell)

cell.selectionStyle = UITableViewCellSelectionStyleNone;

To enable being able to select (tap) the cell: (tapping the cell)

// Default style
cell.selectionStyle = UITableViewCellSelectionStyleBlue;

// Gray style
cell.selectionStyle = UITableViewCellSelectionStyleGray;

Note that a cell with selectionStyle = UITableViewCellSelectionStyleNone; will still cause the UI to call didSelectRowAtIndexPath when touched by the user. To avoid this, do as suggested below and set.

cell.userInteractionEnabled = NO;

instead. Also note you may want to set cell.textLabel.enabled = NO; to gray out the item.

Can I disable the UITableView selection of the individual cell?

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

How can I disable the UITableView selection?

For me, the following worked fine:

tableView.allowsSelection = false

This means didSelectRowAt# simply won't work. That is to say, touching a row of the table, as such, will do absolutely nothing. (And hence, obviously, there will never be a selected-animation.)

(Note that if, on the cells, you have UIButton or any other controls, of course those controls will still work. Any controls you happen to have on the table cell, are totally unrelated to UITableView's ability to allow you to "select a row" using didSelectRowAt#.)

Another point to note is that: This doesn't work when the UITableView is in editing mode. To restrict cell selection in editing mode use the code as below:

tableView.allowsSelectionDuringEditing = false 

How can I disable the UITableView selection?

For me, the following worked fine:

tableView.allowsSelection = false

This means didSelectRowAt# simply won't work. That is to say, touching a row of the table, as such, will do absolutely nothing. (And hence, obviously, there will never be a selected-animation.)

(Note that if, on the cells, you have UIButton or any other controls, of course those controls will still work. Any controls you happen to have on the table cell, are totally unrelated to UITableView's ability to allow you to "select a row" using didSelectRowAt#.)

Another point to note is that: This doesn't work when the UITableView is in editing mode. To restrict cell selection in editing mode use the code as below:

tableView.allowsSelectionDuringEditing = false 

Disable selection of a single UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
UITableViewCell *cell = ...

cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

How to disable tableViewCell selection for a particular part in the UITableView cell iOS

I understand your problem. you have to override the touchesBegan(touches: Set, withEvent event: UIEvent?) method in your custom UITableViewCell.

 class CustomTableViewCell: UITableViewCell {
var isValidTouch: Bool = true

override func touchesBegan(touches: Set, withEvent event: UIEvent?) {

if let touch = touches.first {
let point = touch.locationInView(self.contentView)
let validTouchPoint = self.contentView.bounds.width - (checkMarkView.bounds.width + 16.0) //Trailing + Leading space
if point.x < validTouchPoint {
isValidTouch = true
} else {
isValidTouch = false
//Send tap gesture callback to your viewcontroller
}
}
super.touchesBegan(touches, withEvent: event)
}

}

In your viewcontroller UITableViewDelegate method just check your touch is valid.

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
if cell.isValidTouch {
//do whatever
}
}

Can I disable the UITableView selection of the individual cell?

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

Disable selection of a cell without UserInteractionEnabled in Swift

Implement the UITableView delegate method

tableView(_ tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?

Return nil for the row you don't want to be able to be selected.

IOS Disable UItableview Selection on All Cells but one Cell

You need to do this in the

(UITableViewCell *)tableView:cellForRowAtIndexPath:

callback. This is a setting on a cell that will be executed when it is tapped. It is not something you should try to manually manage

like so:

// Where indexPath != 0 points to the row you want to enable
if( self.selectedInvitedUserId && indexPath != 0 )
{
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
else
{
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
}

Note

This will only stop it being highlighted. You will need to check inside the didSelectRowAtIndexPath: callback if its the correct row.



Related Topics



Leave a reply



Submit