Kauna Mohammed
Kauna Mohammed

Reputation: 1127

How can i stop the background colour of a UIView from disappearing when cell is selected?

I have a tableView which contains a label, image and UIVIew with a red background colour.

This is a picture of a row in my tableView which displays a label, image and a UIView that has a red background colour

This is a picture of a row in my tableView which displays a label, image and a UIView that has a red background colour

But whenever I select a cell, the background colour of the UIVIew disappears and all I can see is the label and image

This is the same row when I select it

This is the same row when I select it

I was wondering if there was a way to stop the background colour of the view from disappearing whenever the cell is selected.

Upvotes: 0

Views: 330

Answers (3)

dengApro
dengApro

Reputation: 4008

It's a little better. I extracted from MBProgressHUD/Demo

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           [tableView deselectRowAtIndexPath:indexPath animated:YES];
       });
   }

Upvotes: 0

Yuyutsu
Yuyutsu

Reputation: 2527

Try this:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      //Just add this below line didSelectRowAt
      tableView.deselectRow(at: indexPath, animated: true)

}

Upvotes: 0

Rakesh Tatekonda
Rakesh Tatekonda

Reputation: 91

It is happening because cell selection style is set to default, change the UITableViewCell selection style to none.

Objective C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Swift 3:

cell.selectionStyle = .none

If you want to notify user has selected the cell, Maintain a property of NSArray which contains list of indexpath object.

Update the NSArray on delegate method didSelectRowAtIndexPath method.

In cellForRowAtIndexPath method change background color if indexpath exists in NSArray

Finally on didSelectRowAtIndexPath reload the selected row.

Upvotes: 1

Related Questions