【问题标题】:UITableView load dataUITableView 加载数据
【发布时间】:2016-05-24 14:57:16
【问题描述】:

我正在尝试在我的应用程序中填充一个表格视图,但我不知道如何创建一个函数来开始填充它。我想这样做是因为我从 http 请求接收数据,但是当 tableview 数据源需要更新时,包含它的数组是空的。 是否可以?我正在使用 Swift。

这是我的代码:

class ContractsViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {

var companyTxt: String = ""
var descriptionTxt: String = ""
var idTxt: String = ""
var dateCreatedTxt: String = ""
var dateExpirationTxt: String = ""
var savedData = Dictionary<Int, JSON>()
var json: JSON = []


@IBOutlet weak var searchContracts: UISearchBar!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var myIndicator: UIActivityIndicatorView!


override func viewDidLoad() {
    super.viewDidLoad()

    preUpload()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func preUpload() {

    let headers = [
        "X-Limit": "5",
        "X-Sort": "{\"expirationDate\":-1}"
    ]


    myIndicator.color = (UIColor.redColor())
    myIndicator.startAnimating()


    Async.background {

        Alamofire.request(.GET, "https://mydata", headers: headers)
            .responseJSON { response in
                switch response.result {
                case .Success (let data):

                    self.json = JSON(data)

                    let replaced = FindAndReplace().findAndReplace(self.json)

                    for (pos, object): (String, JSON) in replaced {
                        print("")
                        print("pos -> " + pos)
                        self.savedData[Int(pos)!] = object
                    }

                case .Failure(let error):
                    print(error)
                }
        }

        }.main {

            self.myIndicator.stopAnimating()

    }


}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.searchContracts.resignFirstResponder()
}


// MARK: TableView


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    //return name.count
    print("Json count -> " + String(json.count))
    return json.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell: ServiceContractsCell? =  tableView.dequeueReusableCellWithIdentifier("cellServiceContracts", forIndexPath: indexPath) as? ServiceContractsCell

    let value = self.savedData[(indexPath.row)]
    let valueMapped = Mapper<ServiceContract>().map(String(value))

    cell?.descriptionText.text = valueMapped?.description



    return cell!
}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
    return false
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
    self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)

}

func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle
{
    return (UITableViewCellEditingStyle.Delete)
}

func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
    return true
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    print("row = %d",indexPath.row)

}



}

提前致谢。

编辑 我不能在 viewDidLoad() 中使用 reloadData() 和 self.tableView.delegate/datasource

【问题讨论】:

  • 获得并解析 JSON 数据后,您需要致电 tableView.reloadData()
  • 正如 Larme 所说,您必须调用 reloadData() 以使表格视图显示新值。
  • 我应该在 .main 中使用 reloadData() 吗?因为如果我尝试这个,我没有异常

标签: ios json swift uitableview tableview


【解决方案1】:

在 cmets 中,您提到如果您尝试重新加载数据,则会出现 nil 异常。确保您的 tableView 已连接到您的 IBOutlet。然后确保你的 viewDidLoad 中有这个。

tableView.delegate = self
tableView.dataSource = self

之后,你应该可以调用

tableView.reloadData()

从主线程

【讨论】:

  • 我在情节提要中匹配并委托,现在我在 viewDidLoad() 中添加了这两行,我尝试在 .main 中调用 tableView.reloadData 但我仍然没有异常,无论如何谢谢跨度>
【解决方案2】:

在您的 Alamo 调用下,将主闭包更新为:

}.main {
self.myIndicator.stopAnimating()
self.tableView.reloadData()
}

【讨论】:

  • 我找不到 reloadTable(),只有 reloadData() 但如果我尝试使用该方法,我没有异常
【解决方案3】:

所有的 UI 更新都应该只在主线程中,您可以拨打tableView.reloadData() 如下所示

dispatch_async(dispatch_get_main_queue()) { () -> Void in
        self.tableView.reloadData()
   }

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多