Pass variable to custom UITableViewCell from UIViewController

前端 未结 4 738
死守一世寂寞
死守一世寂寞 2021-01-20 20:59

Here is how I use my custom UITableViewCell RunningTableViewCell inside UIViewController:

func tableView(tableView: UITableView, ce         


        
相关标签:
4条回答
  • 2021-01-20 21:29

    awakeFromNib is called right after the view and its subviews were allocated and initialized.

    So your code from awakeFromNib in RunningTableViewCell for each cell is called before delegate method func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell. That is why isTop and isBottom are nil in awakeFromNib.

    You can define method in RunningTableViewCell, which will load cell with this variables.

    func load(isTop: Bool, isBottom: Bool) {
        self.isTop = isTop
        self.isBottom = isBottom
    
        // Update cell UI as you wish
    }
    

    And finally rewrite delegate method func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell in your view controller.

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! RunningTableViewCell
    
        let isTop = indexPath.row == 0
        let isBottom = indexPath.row == myArray.count-1
    
        cell.load(isTop, isBottom: isBottom)
    
        return cell
    }
    
    0 讨论(0)
  • 2021-01-20 21:38

    You need to override prepareForReuse in the cell. And remove it from tableView:indexPath:. So when you scroll the cell is going to be reused but the isBotton and isTop vars will be reseted.

    override func prepareForReuse() {
        self.isBottom = false
        self.isTop = false
    }
    
    0 讨论(0)
  • 2021-01-20 21:38

    You can resolve this issue by call custom method in your cell like,

    inside UIViewController:

    //.......
    
    cell.isTop = false
    if(indexPath.row == 0){
        cell.isTop = true
    }
    
    cell.isBottom = false
    if(indexPath.row == myArray.count-1){
        cell.isBottom = true
    }
    
    cell.UpdateViews()
    
    return cell
    }
    

    inside TableViewCell:

    //@IBOutlet ...
    
    var isTop: Bool?
    var isBottom: Bool?
    
    func updateViews() {
      print("result: \(self.isTop) \(self.isBottom)")
    }
    

    Good luck!

    0 讨论(0)
  • 2021-01-20 21:45
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! RunningTableViewCell
    
        //.......
    
        cell.isTop = false
        if(indexPath.row == 0){
            cell.isTop = true
            cell.tag=100
        }
    
        cell.isBottom = false
        if(indexPath.row == myArray.count-1){
            cell.isBottom = true
            cell.tag=200
        }
    
        return cell
    }
    

    and also get this like ...

    class RunningTableViewCell: UITableViewCell {
    
            //@IBOutlet ...
    
            var isTop: Bool?
            var isBottom: Bool?
    
            override func awakeFromNib() {
                super.awakeFromNib()
                if (self.tag==100)
                {
                    isTop=true
                }
                else if (self.tag==200) {
                    isBottom=true
                }
                else{
                    isTop=false
                    isBottom=false
                }
    
                print("result: \(self.isTop) \(self.isBottom)")
            }
        }
    

    And also do using singleton methods...

    0 讨论(0)
提交回复
热议问题