如何访问已创建的实例的属性

时间:2019-06-23 10:27:37

标签: swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = UINavigationController(rootViewController: MainViewController())
        window?.makeKeyAndVisible()
        return true
    }


class MainViewController: UIViewController {

    lazy var mainTV : UITableView = {
        let tv = UITableView()
        tv.delegate = self
        tv.dataSource = self
        tv.register(MainTableViewCell.self, forCellReuseIdentifier: cellId)
        tv.rowHeight = 1000  // property to access
        return tv
    }()
}



extension SubCollectionViewCell: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = todoTV.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! TodoTableViewCell
        cell.textLabel?.text = todoList[indexPath.row]
        cell.textLabel?.numberOfLines = 0


        if indexPath.row == todoList.count - 1 {
            UIView.animate(withDuration: 0, animations: {
                self.todoTV.layoutIfNeeded()
            }) { (complete) in
                var heightOfTableView: CGFloat = 55.0
                let cells = self.todoTV.visibleCells
                for cell in cells {
                    heightOfTableView += cell.frame.height
                }
                //In here, I wanna access property of instance already created (MainViewController.mainTV.rowHeight)
            }
        }


        return cell
    }
}      

2 个答案:

答案 0 :(得分:2)

调用延迟变量属性时,它是tableView的初始化实例。

  • 我认为您应该像这样将tableView实例创建为全局变量:

    import networkx as nx
    import matplotlib.pyplot as plt
    G=nx.Graph()
    
    corr_data =([['Dog', 'Dog', 1.0],
           ['Cat', 'Dog', 0.8016854524612427],
           ['Wolf', 'Dog', 0.5206573009490967],
           ['Person', 'Dog', 0.3756750822067261],
           ['Animal', 'Dog', 0.6618534326553345],
           ['Cat', 'Cat', 1.0],
           ['Wolf', 'Cat', 0.5081626176834106],
           ['Person', 'Cat', 0.32475101947784424],
           ['Animal', 'Cat', 0.6260400414466858],
           ['Wolf', 'Wolf', 1.0],
           ['Person', 'Wolf', 0.23091702163219452],
           ['Animal', 'Wolf', 0.5261368751525879],
           ['Person', 'Person', 1.0],
           ['Animal', 'Person', 0.34220656752586365],
           ['Animal', 'Animal', 1.0]])
    
    corr_data = [[x[0],x[1],1000**(x[2])] for x in corr_data]
    
    existing_edges = {}
    
    def build_graph(w, lev):
      if (lev > 5)  :
          return
      for z in corr_data:
         ind=-1 
         if z[0] == w:
             ind=0
             ind1=1
         if z[1] == w:
             ind ==1
             ind1 =0
    
         if ind == 0 or ind == 1:
             if  str(w) + "_" + str(corr_data[ind1]) not in existing_edges :
    
                 G.add_node(str(corr_data[ind]))
                 existing_edges[str(w) + "_" + str(corr_data[ind1])] = 1;
                 G.add_edge(w,str(corr_data[ind1]))
    
                 build_graph(corr_data[ind1], lev+1)
    
    
    existing_nodes = {}
    def build_graph_for_all():
        count=0
        for d in corr_data:
            if (count > 40) :
                return
            if  d[0] not in existing_edges :
                 G.add_node(str(d[0]))
            if  d[1] not in existing_edges :     
                 G.add_node(str(d[1]))
            G.add_weighted_edges_from([[str(d[0]), str(d[1]),d[2]]])     
            count=count + 1
    
    
    build_graph_for_all()
    
    pos = nx.spring_layout(G,weight='weight')
    nx.draw(G,pos=pos, width=2, with_labels=True)
    
    plt.savefig("path1.png")
    
  • 然后(在cellForRowAt函数中):

    class MainViewController: UIViewController {
    
        lazy var mainTV : UITableView = {
            let mainTableView = UITableView()
            mainTableView.delegate = self
            mainTableView.dataSource = self
            mainTableView.register(MainTableViewCell.self, forCellReuseIdentifier: cellId)
            mainTableView.rowHeight = 1000
            return tv
        }()
    }
    

我希望这个答案对您有用。

编辑:如果要创建具有动态行高的表格视图,则应设置;

mainTableView.rowHeight = 12345

答案 1 :(得分:0)

您尝试过吗?

            let vc = MainViewController()
            let rowHeight = vc.mainTV.rowHeight