简体   繁体   中英

Tapping cell shows the view that is below

I am creating a table view with custom cells.

Every cell has three buttons on it. I have added a view that should occupied the width and height from the cell, overlapping the three buttons. I am using it to create a swipeable cell.

The view that serves as overlay has white color background and opaque, but testing the app on the simulator, I have detected that when tapping on the cell, the buttons are lightly shown, please take a look at the picture.

在此处输入图片说明

What should I do to avoid the buttons to be seen. Why does it happen now if the overlay view is set as opaque. A second issue is when tapping on the cell, the left red bar, also a view, dissapear...

Thank you.

I believe this is a result of UITableViewCell 's default selection styles. Let's try removing the default style and customizing it.

Try setting (depending on your implementation):

  1. cell.selectionStyle = UITableViewCellSelectionStyleNone; inside cellForRowAtIndexPath or
  2. self.selectionStyle = UITableViewCellSelectionStyleNone; in the Cell's custom subclass or
  3. "None" in Interface Builder under "Selection", if you're using IB.

Then, you can implement a custom selection style inside the UITableViewCell's custom class. Inside your initialization method, add something like:

self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds]; self.selectedBackgroundView.backgroundColor = [UIColor lightGrayColor];

The above answer will turn off cell selection highlighting completely and that may not be what you want.

After testing this out several times it appears what's happening is that when a cell is selected, iOS will convert the background color for all views on that cell to clearColor.

So the work around is to simply set the color of your on-cell views back to normal on selection:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(tableView.indexPathForSelectedRow()!) as! MyCell
    cell.MyViewOnMyCell.backgroundColor = UIColor.redColor()
}

See this question for other answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM