2014-11-04 40 views
0

//该objectsAry是NSManagedObjects编程方式更改按钮图像中的表格单元格

//main view controller 

    var nowImg = UIImage(named: "img30.png") 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     var NuTblCellInst = NuTableViewCell() 

     var maincell:UITableViewCell = self.mainList.dequeueReusableCellWithIdentifier("maincell") as NuTableViewCell 

     maincell.textLabel.text = self.objectsAry[indexPath.row].valueForKey("itemname") as? String 

     //NuTblCellInst.doImgLoad(nowImg!) 
     //uncomment the line above = fatal error: unexpectedly found nil while unwrapping an Optional value 
     // it doesn't matter if i use the method or use NuTblCellInst.btnWImg.setBackgroungImage directly (unwrapping error either way) 

     return maincell 
    } 

// custom tableview cell class 

class NuTableViewCell: UITableViewCell { 

    @IBOutlet weak var btnWImg: UIButton! 

    var nowImg = UIImage(named: "img30.png") 

    func doImgLoad(theImg: UIImage) { 
     btnWImg.setBackgroundImage(theImg, forState: UIControlState.Normal) 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     //doImgLoad(nowImg!) 
     //uncomment the line above and it loads the image to the button no problem 
    } 

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

我填充一个可重复使用的盒W /文本如上,但不知道如何访问图像(或背景图像),放置在表格单元格中的按钮,使用故事板,其中图像随表格行而变化。

回答

0

从'...将随表格行不同而变化',这听起来像是您正在构建可重复使用的模板单元格。

  1. CustTableViewCell必须从UITableViewCell
  2. 编辑您的模板细胞在故事板编辑器中的身份检查得出...
    • ,班级设置为自定义类
    • 创建一个出口按钮CustTableViewCell
  3. tableview:cellForRowAtIndexPath
    • 创建/出队maincell作为CustTableViewCell - 按钮通过出口
    • 回报您的实例(当然:-))

UITableViewCell

  • 组属性cf custom uitableviewcells in storyboard

  • +0

    会回来。可能还要等一下。那么,如果我已经返回maincell,如何返回CFRAIP中的自定义类实例? – agent86 2014-11-04 21:20:39

    +0

    你的CFRAIP现在是什么样的? – 2014-11-04 21:46:52

    +0

    新增了解答的更多详细信息 – 2014-11-04 22:45:40

    相关问题