1.问题描述:自己在做之前iOS项目之Swfit新闻App的时候,发现解析出来的Json串中的model数据存在不一致的情况,就是说有的cell有label和图片,有的cell仅仅只有label。那如何处理这种情况呢?
解决方案:我在用Alamofire网络库对url进行解析的时候,对解析出来的数据会进行一次判断,如果某一项解析出来的imageUrl为nil的话,我会对model中对应的某项的imageUrl设置为”empty”,然后在TableView的绘制cell的回调方法中对某一行的cell进行赋值的时候,对imageUrl进行判断如果url为empty,则只为该cell的label进行赋值运算,反之则都赋值。这样就可以处理cell中内容不一致的情况。部分代码如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let initIdentifier = "MyThemeCell"
var cell = tableView.dequeueReusableCellWithIdentifier(initIdentifier) as? ThemeDataTableViewCell
let url = self.item[indexPath.row].newsImageName
if cell == nil{
tableView.registerNib(UINib(nibName: "ThemeDataTableViewCell", bundle: nil), forCellReuseIdentifier: initIdentif