【问题标题】:UITableView in UIView with swiftUIView中的UITableView与swift
【发布时间】:2015-06-27 01:38:01
【问题描述】:

我整天都被这个问题所困扰,仍然不知道为什么这个问题如此难以解决。我一直在尝试让一个简单的 UITableview 工作。我设法让它出现在屏幕上(悲伤的泪水),但我无法让表格视图更新或做任何事情。

请务必注意,我的 UITableView 位于我在 UIViewController 中调用的 UIView 类中。

这是我的 UIView 类:

import Foundation
import UIKit

class comebackViewController:UIView{
@IBOutlet var table: UITableView!

 func setup(){

     table = UITableView()

 }

}

这是我的主要 UIViewController:

import UIKit

class ViewCont: UIViewController, UITableViewDataSource, UITableViewDelegate{
var tableViewCont:comebackViewController!
var ListArray:NSMutableArray = []

override func viewDidLoad() {
    super.viewDidLoad()
    ListArray.addObject("hello")
    ListArray.addObject("goodbye")

    print(ListArray.count)

    tableViewCont = NSBundle.mainBundle().loadNibNamed("comebackTableView", owner: self, options: nil).last as! comebackViewController
    tableViewCont.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.height)
    tableViewCont.table = UITableView()
    tableViewCont.setup()
    tableViewCont.table.dataSource = self
    tableViewCont.table.delegate = self
    tableViewCont.table.registerClass(tableViewCellController.self, forCellReuseIdentifier: "cell")

    //Some people say you have to call reloadData like this...
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.tableViewCont.table.reloadData()
        //let indexPath = NSIndexPath(forRow: 0, inSection: 0)
        //self.tableViewCont.table.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
    })

    tableViewCont.table.reloadData()

    self.view.addSubview(tableViewCont)

}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    print("did get count...\n")
    return self.ListArray.count;
}

func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    print("did get height...\n")
    return 44
}

func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    print("did get number of sections...\n")
    return 2
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    print("table did reload...\n")
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! tableViewCellController

    cell.random1.text = "\(ListArray.objectAtIndex(indexPath.row))"
    cell.random2.text = "me"
    /*
    if let desc = self.items[indexPath.row]["description"] as? NSString   {
    cell.detailTextLabel.text = desc
    }*/

    return cell
}

}

当我尝试运行此代码时,终端会打印出来

did get number of sections...
did get count...
did get height...
did get height...
did get count...
did get height...
did get height...

这意味着当我调用 reloadData() 函数时,它会被调用,但表中没有任何内容。另外,当我调用 reloadData() 时,我从来没有看到“table did reload ...\n”打印到屏幕上。这意味着永远不会向表格中添加任何单元格。

注意:我已经在 Objective-c 中完成了这项工作,但 swift 对我来说似乎有点困难。而且我知道有人会告诉我,我必须有一个 UITableViewController,但表必须在我的应用程序的单独 UIView 中,谢谢!

【问题讨论】:

  • 检查你的tableView的大小,可能是零。

标签: swift uitableview uiview uiviewcontroller


【解决方案1】:

出现这种行为是因为 tableView 的大小为零:

tableViewCont = NSBundle.mainBundle().loadNibNamed("comebackTableView", owner: self, options: nil).last as! comebackViewController
tableViewCont.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.height)
tableViewCont.table = UITableView()
tableViewCont.setup() // you instantiate a new UITableView

你应该在实例化后设置它的框架:

tableViewCont = NSBundle.mainBundle().loadNibNamed("comebackTableView", owner: self, options: nil).last as! comebackViewController
tableViewCont.setup()
tableViewCont.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.height)

【讨论】:

  • 这很棒!我可能永远不会发现,谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
相关资源
最近更新 更多