gpt4 book ai didi

swift - 对部分中成员 TableView 编号行的引用不明确

转载 作者:行者123 更新时间:2023-11-28 08:38:25 25 4
gpt4 key购买 nike

我正在从 Web 服务器加载一些数据,但我对成员 TableView numberOfRowsInSection 的引用不明确?下面是我的 swift 文件。我在这里错过了什么?

TableViewController.swift

import UIKit

class TableViewController: UITableViewController {
var viewModel:ViewModel!

override func viewDidLoad() {
super.viewDidLoad()

self.viewModel = ViewModel()
self.tableView.delegate = viewModel
self.tableView.dataSource = viewModel

self.tableView.registerNib(UINib(nibName: "TableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: tableViewCellIdentifier)

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 50;
}

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


}

ViewModel.swift

import UIKit

class ViewModel: NSObject,UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource {


var dataArrayPosts: NSArray!
var posts:[Post] = []

override init() {
super.init()


let myURL = NSURL(string: "mydomainURLhere");
let requestPosts = NSMutableURLRequest(URL: myURL!);
requestPosts.HTTPMethod = "POST";

let postStringVars = ""


requestPosts.HTTPBody = postStringVars.dataUsingEncoding(NSUTF8StringEncoding);

let taskPosts = NSURLSession.sharedSession().dataTaskWithRequest(requestPosts){ data, response, error in

if error != nil{

print("error\(error)");
return;
}

do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
//print(json)

if let category = json["Posts"] as? NSMutableArray{
//print(category)

_ = category

self.dataArrayPosts = json["Posts"] as! NSMutableArray;



dispatch_async(dispatch_get_main_queue()) {

self.posts.removeAll()


for item in self.dataArrayPosts {


let post = Post(postDesc: (item["postDesc"] as? String)!,imageName: (item["imageName"] as? String)!,bName: (item["bName"] as? String)!,postDate: (item["postDate"] as? String)!,postShowsUntil:(item["postShowsUntil"] as? String)!)

self.posts.append(post)
// Ambiguous on reloadData???????
self.tableView.reloadData()

}
}
}else{

dispatch_async(dispatch_get_main_queue()) {
print("Nothing Was Found")

}

}

}
} catch let error as NSError {

print(error.localizedDescription)
}




}

taskPosts.resume()




}




func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(tableViewCellIdentifier, forIndexPath: indexPath) as! TableViewCell

let post = posts[indexPath.row]
cell.quoteTextLabel.text = post.postDesc
cell.nameLabel.text = post.bName

if let imageName = post.imageName where !imageName.isEmpty{
cell.photoView?.image = UIImage(named: imageName)
cell.photoWidthConstraint.constant = kDefaultPhotoWidth
cell.photoRightMarginConstraint.constant = kDefaultPhotoRightMargin
}
else {
cell.photoView?.image = nil
cell.photoWidthConstraint.constant = 0
cell.photoRightMarginConstraint.constant = 0
}

cell.contentView.setNeedsLayout()
cell.contentView.layoutIfNeeded()

return cell
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewCellIdentifier, forIndexPath: indexPath) as! CollectionViewCell

return cell
}







}

enter image description here

最佳答案

ViewModel 继承自的所有内容均未公开 tableView 属性,您的 ViewModel 需要引用您的 UITableViewController。由于 ViewController 拥有 ViewModel,因此它应该是一个弱引用,如下所示

class ViewModel: NSObject,UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource {

weak let tableViewController:UITableViewController

var dataArrayPosts: NSArray!
var posts:[Post] = []

override init(controller:UITableViewController) {
self.tableViewController = controller
super.init()
// ...
self.tableViewController.tableView.reloadData()
}

class TableViewController: UITableViewController {
var viewModel:ViewModel!

override func viewDidLoad() {
super.viewDidLoad()

self.viewModel = ViewModel(controller:self)
// ...
}

关于swift - 对部分中成员 TableView 编号行的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37032259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com