ios - UITableViewCell only displays accessory after scrolling -


so have tableview custom cells make fetching data server. have variable 'selectedindex' use keep track , add checkmark accessory cell. weirdly, works after scroll selected cell (cell indexpath.row equal off screen , back. here code in willdisplaycell method:

if selectedindex == indexpath.row {     if let accessory = cell.viewwithtag(528) as? uitableviewcell {         accessory.frame = (cell.checkmarkview.bounds.offsetby(dx: 0, dy: 7))         accessory.accessorytype = .checkmark         accessory.isuserinteractionenabled = false         accessory.backgroundcolor = uicolor.clear         accessory.tag = 528         accessory.ishidden = false         print("accessory not nil")      } else {         let accessory = uitableviewcell()         accessory.frame = (cell.checkmarkview.bounds.offsetby(dx: 0, dy: 7))         accessory.accessorytype = .checkmark         accessory.isuserinteractionenabled = false         accessory.backgroundcolor = uicolor.clear         accessory.tag = 528         accessory.ishidden = false         cell.addsubview(accessory)          print("accessory nil")              }  } else {      let accessory = cell.viewwithtag(528)      accessory?.ishidden = true  } 

for example, when selected index 0, checkmark not displayed @ first view, , logs print ("accessory nil"). when scroll cell @ index 0 off-screen, , scroll again, checkmark displayed , logs print ("accessory not nil"). more information, tapping on cell works expected.

edit:

  1. i know i'm adding tableviewcell cell! i'm doing because need checkmark accessory in different position (left-aligned, top-aligned) default 1 (right-aligned, vertically centered). did add view in xib in desired position (right-aligned, top-aligned) , and aligned programatically-made cell it. if show me approach appreciated.

  2. removed cell.addsubview first if. still behaves same.

thanks!

you adding uitableviewcell dequeued original cell, unnecessary, need implement logic in cellforrowat datasource method,

updated

if need custom position accessory view use cell.layoutmargins check updated code

try instead

func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {          let cell = tableview.dequeuereusablecell(withidentifier: "yourcellidentifier", for: indexpath) as! yourcellclass          cell.accessorytype = .none         if selectedindex == indexpath.row {               cell.accessorytype = .checkmark         }         //if need custom position accessory view         cell.layoutmargins = uiedgeinsetsmake(0, 0, 0, 100)     } 

enter image description here


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -