简体   繁体   中英

Swift - tableviewcell returns empty using custom cell

I am new to swift programming and would need some help to check what is wrong in my tableviewcell. I have tried alot of great suggestions on stackoverflow.( make sure your outlets are connected, set delegate and datasource of your tableview to self)

This is my 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) {

 }

}

This is my subclass for my 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

}

}

This is my Model:

import Foundation


struct  CharacterListModel {



var name: String?

var characterID: String?

var location: String?

var lastlocatedtime: String?

var imagebutton: UIImage?



var sos: Bool?

}

This is my 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
}

Thanks in advance!

Don't fetch data in viewDidLoad() method. Fetch in viewWillAppear and reload after getting data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM