2017-06-16 165 views
-1

我在自定义单元格在tableView中遇到问题。我正在做的是,我访问我的数据库,并在数据库中的每一行,我会填写一个单元格。问题是,它只会为每个单元格反复打印最后一行;然后,我尝试使用过滤器(通过ID),但它只打印每个单元格的第一行。 我事先感谢您的帮助。我的代码如下自定义表格单元格问题

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
var x = Int() 
    do{ 
    let count = try conn.db!.scalar(tblPersona.count) 
    x = count 
    } 
    catch{ 
    } 
    return x 
} 



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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! consultaInformacionTableViewCell 
    var res = Int64 (1) 
    do{ 

     let query = tblPersona.filter(id == res) 
     for custom in try conn.db!.prepare(query){ 
      print(custom[nombre]!) 

      cell.nombreLabel.text = custom[nombre]! 
      cell.emailLabel.text = custom[email]! 
      res = res + 1 
     } 
    } 
    catch{ 

    } 
    return cell 
} 

当我打印“custom [nombre]!”时,它会在每次完成一个循环时打印所有行。

回答

0

您的筛选条件似乎不正确。

tblPersona.filter(id == res) 

比较首先解决。所以,你有效地说:

tblPersona.filter(true) 

看起来“真”是返回每个条目。但是,不知道您使用的过滤器的类型,我无法提供一个工作示例。

+0

现在你提到它,有道理为什么程序只打印第一行,但我不明白为什么“res”不会在句子末尾增加 –

+0

从我所看到的,它应该是?你是否已经通过代码来验证每个循环都不会递增? – Software2

+0

我使用断点,每次新循环开始时,“res”等于1。 –