Swift - tableviewcell使用自定义单元格返回空

时间:2018-01-18 02:13:08

标签: ios swift

我是快速编程的新手,需要一些帮助来检查我的tableviewcell中的错误。我已经在stackoverflow上尝试了很多很棒的建议。(确保你的出口连接,将你的tableview的委托和数据源设置为自己)

这是我的ViewController:

import UIKit


class CharacterViewController: UIViewController, UITableViewDataSource, UITableViewDelegate , APIControllerProtocol {



@IBOutlet weak var  CharacterInfoView: UITableView!



var apiController:APIController!

var dataArray: [[String:Any]]?

var processcharacter= [CharacterListModel]()



override func viewDidLoad() {

    super.viewDidLoad()




    self.view.backgroundColor=UIColor.white

    self.CharacterInfoView.estimatedRowHeight = 44

    self.CharacterInfoView.rowHeight = UITableViewAutomaticDimension

    self.CharacterInfoView.dataSource = self

    self.CharacterInfoView.delegate = self



    apiController = APIController()

    apiController.delegate=self



    self.navigationItem.title =  "Character"

    self.view.showLoading()



    apiController.getCharacterData{ (statusCode, data, response, error) -> () in

     self.view.stopLoading()



     if(statusCode == nil)

     {

     self.view.showServiceNotAvailableMessage(self)

     }



     if !(error == nil)

     {

     self.view.showServiceNotAvailableMessage(self)

     }



     if statusCode == 200

     {

     do

     {



        self.processcharacter= CharacterListData.processData(data: data)

        self.CharacterInfoView.reloadData()



     }

     catch(_ as NSError)

     {



     }

     }

     else

     {



     return

     }

     }

}



func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1

}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    if self.dataArray == nil

     {

        return 0

     }

     else

     {

        return processcharacter.count

     }

}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



    let cell = tableView.dequeueReusableCell(withIdentifier:"Cell", for:indexPath) as! CharacterTableViewCell



    var processcharacters= processcharacter[indexPath.row]

    cell.location.text = processcharacters.location

    cell.name.text = processcharacters.name

    cell.characterID.text = processcharacters.characterID

    cell.time.text = processcharacters.lastlocatedtime



    if wecares.sos == true {

        processcharacters.imagebutton = UIImage(named: "sos_icon")!

    }

    else{

        processcharacters.imagebutton = UIImage(named: "null_button")!

    }



    cell.button.image = processcharacters.imagebutton



    return cell

}

func reachabilityChanged(_ status: Bool) {

 }

}

这是我的ViewController的子类:

import Foundation


class CharacterListDataHelper: NSObject {

static func processData(data: AnyObject?) -> [CharacterListModel]

{



    var modelList:[CharacterListModel] = [CharacterListModel]()

    let darr = try? JSONSerialization.jsonObject(with: data! as! Data, options: .mutableLeaves) as! [[String:Any]]

    var dataModel:CharacterListModel



    for obj in darr!

    {

        dataModel = CharacterListModel()

        dataModel.location = obj["playerLocation"] as! String

        dataModel.name = obj["playerName"] as! String

        dataModel.characterID= obj["playerID"] as! String     

        dataModel.lastlocatedtime = obj["lastUpdatedTime"] as! String

        }

        modelList.append(dataModel)

    }

    return modelList

}

}

这是我的模特:

import Foundation


struct  CharacterListModel {



var name: String?

var characterID: String?

var location: String?

var lastlocatedtime: String?

var imagebutton: UIImage?



var sos: Bool?

}

这是我的TableViewCell:

class CharacterTableViewCell: UITableViewCell {

@IBOutlet weak var name: UILabel!
@IBOutlet weak var characterID: UILabel!
@IBOutlet weak var location: UILabel!
@IBOutlet weak var lastlocatedtime: UILabel!
@IBOutlet weak var button: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

不要在 viewDidLoad()方法中获取数据。获取 viewWillAppear 并获取数据后重新加载。