【发布时间】: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