我想在更改分段控件时从Web服务更新tableview。我的代码已触发事件 segmentedControlAction 以更新 viewDidAppear 事件中的tableview。 它首次在屏幕加载时工作但在我选择下一个选项卡(以下)时无效,它从Web服务获取数据而不是更新tableview。但当我切换到第一个追随者选项卡,然后再来到以下选项卡它可以工作,因为它存储从previuos Web服务获取的数据
数据源( ds )是从网络服务获取数据,如果它有零记录
TsButton是自定义类,只有一个成员变量 object ,用于将额外信息传递给按钮点击事件
在StoryBoard中,有两个控件分段控件和桌面视图,如屏幕截图
我的代码在下面
class UserFollowController: BaseViewController
{
@IBOutlet var segmentedControl: UISegmentedControl!
@IBOutlet var tableView: UITableView!
private var ds : UserFollowTableDataSource!
override func viewDidLoad()
{
super.viewDidLoad()
ds = UserFollowTableDataSource(vc: self, tableView: tableView);
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if UserEntity.instance.username.characters.count == 0
{
let storyBoard : UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "LoginController") as! LoginController
self.tabBarController?.selectedIndex = 0
self.present(vc, animated: true, completion: nil)
}
else
{
self.segmentedControlAction(sender: nil);
}
}
@IBAction func segmentedControlAction(sender: AnyObject?) {
if(segmentedControl.selectedSegmentIndex == 0)
{
ds.fetchFromWeb(type: "follower")
}
else
{
ds.fetchFromWeb(type: "following")
}
}
}
class UserFollowTableDataSource : NSObject, UITableViewDataSource
{
private var follower_records : [[String : Any]]?;
private var following_records : [[String : Any]]?;
private var records : [[String : Any]]?;
private var tableView : UITableView!;
private var vc : UserFollowController!;
private var loader : UIAlertController!;
private var type : String!
init(vc : UserFollowController, tableView : UITableView)
{
self.vc = vc;
self.tableView = tableView
self.loader = CommonUtil.getLoader(msg: "Getting List...");
super.init()
self.tableView.dataSource = self;
}
public func fetchFromWeb(type : String)
{
self.type = type;
self.loader.title = "Getting List...";
if (type == "follower")
{
if let data = self.follower_records
{
self.records = data;
self.tableView.reloadData();
self.tableView.setNeedsDisplay();
return;
}
self.vc.present(self.loader, animated: true, completion: nil)
HauteWebService.instance.get_user_followers(onSucess: { (response) in
if (response.status == 1)
{
DispatchQueue.main.async {
self.loader.dismiss(animated: true, completion: {
self.follower_records = response.data as! [[String : Any]]
self.records = self.follower_records
self.tableView.reloadData();
});
}
}
else
{
self._onError(msg: response.msg);
}
}, onFailure: { (msg) in
self._onError(msg: msg);
})
}
else
{
if let data = self.following_records
{
self.records = data;
self.tableView.reloadData();
self.tableView.setNeedsDisplay();
return;
}
self.vc.present(self.loader, animated: true, completion: nil)
HauteWebService.instance.get_user_following(onSucess: { (response) in
if (response.status == 1)
{
DispatchQueue.main.async {
self.loader.dismiss(animated: true, completion: {
self.following_records = response.data as! [[String : Any]]
self.records = self.follower_records
self.tableView.reloadData();
})
}
}
else
{
self._onError(msg: response.msg);
}
}, onFailure: { (msg) in
self._onError(msg: msg);
})
}
}
private func _onError(msg : String)
{
DispatchQueue.main.async {
self.loader.dismiss(animated: true, completion: {
let alert = CommonUtil.getAlertError(title: "Error while getting web service", msg: msg);
self.vc?.present(alert, animated: true, completion: nil)
})
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.records != nil
{
return self.records!.count;
}
return 0;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "FollowTableCell", for: indexPath) as! FollowTableCell
let index = indexPath.row;
cell.tag = index;
if let record = self.records![index] as? [String : Any]
{
let first = record["firstname"] as! String
let last = record["lastname"] as! String
cell.lblName.text = first + " " + last;
var add = record["city"] as! String;
let state = record["state"] as! String;
if state != ""
{
add = add + ", " + state
}
cell.lblAddress.text = add;
cell.btnFollow.backgroundColor = TFColor.hexToUIColor(hex: "#AAAAAA")
cell.btnFollow.setTitle("Follow", for: .normal)
cell.btnFollow.object = record;
if let v = record["is_followed"] as? Int
{
if (v == 1)
{
cell.btnFollow.backgroundColor = TFColor.hexToUIColor(hex: "#887474")
cell.btnFollow.setTitle("Following", for: .normal)
}
}
cell.btnFollow.addTarget(self, action: #selector(self.btnFollowClick), for: .touchUpInside)
cell.ratingStar.changeEvent = false;
if let v = Float(record["rating"] as! String)
{
cell.ratingStar.rating = Int(v)
}
else
{
cell.ratingStar.rating = 0;
}
cell.imgProfile.image = UIImage(named: "no_image");
if let url = record["profile_image_url"] as? String
{
if let img = ImageEntity.readCache(key: url)
{
cell.imgProfile.image = img
}
else
{
HauteWebService.instance.downloadFile(url: url, onSucess: { (data) in
if let img = UIImage(data: data)
{
ImageEntity.writeCache(key: url, image: img)
DispatchQueue.main.async {
if cell.tag == index
{
cell.imgProfile.image = img
}
}
}
}, onFailure: { (msg) in
})
}
}
}
return cell;
}
@objc func btnFollowClick(_ sender: TsButton)
{
let record = sender.object as! [String : Any];
let id = record["id"] as! String;
let data = ["user_id" : id]
if let v = record["is_followed"] as? Int
{
self.loader.title = "Please Wait...";
vc.present(self.loader, animated: true, completion: nil);
if (v == 1)
{
HauteWebService.instance.delete_user_follow(data: data, onSucess: { (response) in
if (response.status == 1)
{
DispatchQueue.main.async {
self.loader.dismiss(animated: false, completion: {
DispatchQueue.main.async {
self.follower_records = nil
self.following_records = nil
self.fetchFromWeb(type : self.type)
}
});
}
}
else
{
self._onError(msg: response.msg)
}
}, onFailure: { (msg) in
self._onError(msg: msg);
})
}
else
{
HauteWebService.instance.save_user_follow(data: data, onSucess: { (response) in
if (response.status == 1)
{
DispatchQueue.main.async {
self.loader.dismiss(animated: false, completion: {
DispatchQueue.main.async {
self.follower_records = nil
self.following_records = nil
self.fetchFromWeb(type : self.type)
}
});
}
}
else
{
self._onError(msg: response.msg)
}
}, onFailure: { (msg) in
self._onError(msg: msg);
})
}
}
}
}
class FollowTableCell : UITableViewCell
{
@IBOutlet var imgProfile : UIImageView!
@IBOutlet var actvityImgProfile : UIActivityIndicatorView!
@IBOutlet var lblName : UILabel!
@IBOutlet var lblAddress : UILabel!
@IBOutlet var btnFollow : TsButton!
@IBOutlet var ratingStar : TsRatingStar!
}
答案 0 :(得分:1)
在您的以下网络服务回调中,您有
self.following_records = response.data as! [[String : Any]]
self.records = self.follower_records
因此,您正在将webView的记录设置为webservice回调中的错误值。
应该是:
self.following_records = response.data as! [[String : Any]]
self.records = self.following_records