自定义单元格在TableView Swift 3中返回空白

时间:2017-01-14 23:55:38

标签: ios swift xcode uitableview

所以我目前正在尝试使用基于API调用的自定义单元格填充tableview。在我获取数据后,我重新加载tableview。它有基于它有多少个单元格的数据,但每个单元格都是空白的。我已经查看过人们遇到的一些类似问题,但似乎找不到修复方法。任何帮助都会很棒,谢谢!

我调用我的API并收集我需要的数据。然后我重新加载了tableview。

tabChanged = (tabChangeEvent: MatTabChangeEvent): void => {
  console.log('tabChangeEvent => ', tabChangeEvent);
  console.log('index => ', tabChangeEvent.index);
}

这是自定义Cell Class

class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var tableView: UITableView!
@IBOutlet var typeSwitch: UISegmentedControl!
@IBOutlet var searchBar: UITextField!
var timeoutTracker = 0
var items: [String] = []
var count = 1
func searchCall() {
    Alamofire.request("http://www.plex.dev/api/search/" + type + "/" + searchBar.text!).responseJSON { response in
        debugPrint(response)
        if response.response == nil {
            self.timeoutTracker = self.timeoutTracker + 1
            if self.timeoutTracker == 5 {
                self.timeoutTracker = 0
                self.popUp()
            }
            else {
                sleep(2)
                self.searchCall()
            }
        }
        if let json = response.result.value {
            let jsonTest = JSON(json)
            for x in jsonTest["results"] {
                if let title = x.1["title"].string {
                    self.items.append(title)
                    self.tableView.delegate = self
                    self.tableView.dataSource = self
                    self.tableView.reloadData()
                }
            }
        }
    }
}
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
    return self.items.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    var cell = self.tableView.dequeueReusableCell(withIdentifier: "cell")! as! MovieCell
    cell.movieTitle?.text = self.items[indexPath.row]
    cell.moviePoster?.image = #imageLiteral(resourceName: "avatar.png")
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("You selected cell #\(indexPath.row)!")
}

这是我的故事板: enter image description here

这是我的空白桌面视图: enter image description here

我希望这不是显而易见的事情,但无论如何都要感谢你的帮助!

编辑:将我的标识符更改为“单元格”以外的其他内容似乎可以解决问题

3 个答案:

答案 0 :(得分:0)

你在任何地方调用searchCall()吗? 也建议你申报

 tableView.delegate = self
 tableView.dataSource = self

在SearchViewController的viewDidLoad()中添加UITableView的委托和数据源作为SearchViewController的扩展(这是一个更好的代码实践)

编辑:再看一下你的cellForRowAt:尝试用以下代码替换你的代码

 let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MovieCell

并且不使用self.tableView这里

编辑2:有时使用默认的单元格标识符'cell'可能会导致一些问题,所以最好使用另一个例如'movieCell'

答案 1 :(得分:0)

我怀疑您正在使用表视图注册类或nib,但是当您使用原型单元时,不应该这样做。此外,您应该使用更现代的dequeueReusableCell(withIdentifier: String , for indexPath: IndexPath)。此外,我会抵制诱惑sleep

答案 2 :(得分:0)

您是否将课程提供给单元格并连接单元格中的出口