TableView单元格搞乱约束

时间:2015-11-06 13:39:15

标签: iphone swift uitableview tableviewcell

我有一个tableView,我从我的数据库中提供新闻源。现在的细胞数量大约是20,但它肯定会继续增长。

在一个单元格中,我有三个标签:标题,日期和简短说明。我使用故事板设置了所有约束。我的tableView单元格的高度根据我的内容大小动态变化。

问题是每当我加载我的Feed时,其中一个单元格总是搞砸了。标题标签和日期标签之间存在约束> = 30因此它始终至少有一个单元格具有恒定宽度为190的标题标签(其他标签始终正确显示)。如果我向上滚动tableView并返回到该单元格,它将恢复正常,但单元格的高度将保持不变(对于内容来说太大)。

here This is how it looks on the first load

导致问题的原因是什么?

提前谢谢!

修改

This is how it looks if I scroll down to the end of the tableView and back This is how it looks in storyboard

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {




    //let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath)
    let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! FinalNews

    cell.backgroundColor = UIColor(red: 249/255, green: 237/255, blue: 229/255, alpha: 1)

    let chevron = UIImage(named: "Next4.png")
    cell.accessoryType = .DisclosureIndicator
    cell.accessoryView = UIImageView(image: chevron!)

    cell.textLabel?.textColor = UIColor.blackColor()
    // Feeds dictionary.
    var dict : NSDictionary! = myFeed.objectAtIndex(indexPath.row) as! NSDictionary

    let firstiteration: String = (myFeed.objectAtIndex(indexPath.row).objectForKey("Description")?.stringByReplacingOccurrencesOfString("<b>", withString: ""))!
    let seconditeration = firstiteration.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)


    let category: String = myFeed[indexPath.row].objectForKey("CategoryID") as! String

    func getCategory(value: String) -> String {
        var end = value.endIndex
        for var i = value.startIndex;
            i < value.endIndex;
            i = i.successor() {
                if value[i] == "\n" {
                    end = i
                    break
                }

        }
        return value[value.startIndex..<end]
    }


    let counter1 = getCategory(category)

    if counter1 == "1"{
        cell.titleLabel.text = myFeed.objectAtIndex(indexPath.row).objectForKey("DosNum") as? String
        cell.descLabel.text = " \n "
        cell.descLabel.textColor = UIColor(red: 249/255, green: 237/255, blue: 229/255, alpha: 1)
        cell.dateLabel.text = myFeed.objectAtIndex(indexPath.row).objectForKey("Date_text") as? String
    }
    else{

        cell.titleLabel.text = myFeed.objectAtIndex(indexPath.row).objectForKey("Title") as? String
        cell.descLabel.text = seconditeration
        cell.descLabel.textColor = UIColor.blackColor()
        cell.dateLabel.text = myFeed.objectAtIndex(indexPath.row).objectForKey("Date_text") as? String

    }

    var shorDate: String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        return dateFormatter.stringFromDate(NSDate())
    }
    let today1 = shorDate
    let dateIter = myFeed.objectAtIndex(indexPath.row).objectForKey("Date_publication") as? String

    let dateIter1: String = (dateIter as? NSString)!.substringToIndex(10)

    let calendar = NSCalendar.currentCalendar()


    //let twoDaysAgo = calendar.dateByAddingUnit(.Day, value: -1, toDate: NSDate(), options: [])
    var shorDate1: String {
        let dateFormatter = NSDateFormatter()
        let oneDaysAgo = calendar.dateByAddingUnit(.Day, value: -1, toDate: NSDate(), options: [])
        dateFormatter.dateFormat = "yyyy-MM-dd"
        return dateFormatter.stringFromDate(oneDaysAgo!)
    }

    let oneDaysAgo1 = shorDate1


    if today1 == dateIter1{
        cell.dateLabel.text = "Сегодня"
        //someDate is berofe than today
    } else if dateIter1 == oneDaysAgo1 {
        //someDate is equal or after than today
        cell.dateLabel.text = "Вчера"
    } else {
        cell.dateLabel.text = myFeed.objectAtIndex(indexPath.row).objectForKey("Date_text") as? String
    }

    return cell

}

0 个答案:

没有答案