when uitableviewcell call method which is reloadcell,customviewcell.awakeformxib is not called

Hi there



I was wondering if i would like to post question.



When my application create data which is used on uitableviewcell on another viewcontroller and go back to ViewController with uitableview,

i want to update specification cell,however it can't run correctly what i expected.



This following code is reload cell which i want to reload

--------------------------------------------------------------------------

//こだわりだけリロード
func updateOtherKodawari(){
    let indexpath = NSIndexPath(forRow: 0, inSection: 5)
    self.appdelegate.shotvm.readData()
    self.menuview.beginUpdates()
    self.menuview.reloadRowsAtIndexPaths([indexpath], withRowAnimation: UITableViewRowAnimation.None)
    self.menuview.endUpdates()
}




case 5:
        let cell: OtherKodawariCell = tableView.dequeueReusableCellWithIdentifier("OtherKodawariCell", forIndexPath: indexPath) as! OtherKodawariCell
        return cell

This following code is within uitableview cell.



import UIKit


class OtherKodawariCell: UITableViewCell {


weak var appdelegate:AppDelegate!




override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    self.appdelegate = AppDelegate.sharedAppDelegate()
    setUp()
}


func setUp(){


    var x = 0
    let top_margin_x = 8
    let margin_x = 10
    var y = 5
    let margin_y = 5
    let screen = UIScreen.mainScreen()
    let width = Int((screen.bounds.size.width-30)/2)
    let height = 20


    let ary_other_name_kodawari = self.appdelegate.appCode.dic_cond?.objectForKey("ary_other_name_kodawari") as? NSArray


    let ary_other_kodawari = self.appdelegate.appCode.dic_cond?.objectForKey("ary_other_kodawari") as? NSArray


    //空の場合は終了
    if ary_other_name_kodawari != nil{
        if ary_other_name_kodawari!.count == 0{
            return
        }
    }else{
        return
    }




    if let array = ary_other_kodawari{
        var i = 0
        for code in array{


            if i%2 == 0 && i != 0{
                y += margin_y+height
                x = top_margin_x
            }else if i == 0{
                x += top_margin_x
            }
            let text: AnyObject? = ary_other_name_kodawari?.objectAtIndex(i)
            if let font = UIFont(name: "HiraKakuProN-W6", size: 10){
                let size:CGSize = text!.sizeWithAttributes([NSFontAttributeName:font])
                let tagview = TagView(frame: CGRect(x: x, y: y, width: width, height: height))
                tagview.setUp(text! as! String, withfont: font, withsize: size)
                self.addSubview(tagview)
                x += width
            }
            x += margin_x
            i += 1
        }
    }
}




override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)


    // Configure the view for the selected state
}


}

In my expectation,cell re-loada when cell reload,then awakefromxib function is going to call.

is it wrong?

awakefromxib sometimes call when cell reload,but it sometimes call when cell reload.



Thank you.

Answered by just.do.it in 13598022

Hi,


awakeFromNib is only called if a UITableViewCell is created. But as UITableViewCells are reused (which means they arn't deleted when becoming invisible but put into a reuse-array)

If you want to execute code each time a cell is being reused "prepareForReuse" method is your friend.


Dirk

Accepted Answer

Hi,


awakeFromNib is only called if a UITableViewCell is created. But as UITableViewCells are reused (which means they arn't deleted when becoming invisible but put into a reuse-array)

If you want to execute code each time a cell is being reused "prepareForReuse" method is your friend.


Dirk

Thank you for replying!

I understand!

I will try it.

when uitableviewcell call method which is reloadcell,customviewcell.awakeformxib is not called
 
 
Q