滚动

时间:2015-07-17 08:28:13

标签: ios swift uitableview uicollectionview

我在tableViewCell中嵌入了一个collectionView。我从this回答了一个例子,除了我希望我的collectionViewCells大小不同。这里是我计算tableViewRowHeight并在collectionView中标记框架的代码。对不起我的硬编码,我只是想以某种方式去做(。

以下是代码:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    var cellHeight: CGFloat = 0.0
    let screenWidth: CGFloat = UIScreen.mainScreen().bounds.height

    var question: NSString = events[indexPath.row].category
    var size = CGSizeMake(screenWidth * 0.2, 9999)
    var textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    cellHeight = size.height

    question = events[indexPath.row].event
    size = CGSizeMake(screenWidth * 0.3, 9999)
    textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    if cellHeight < size.height {
        cellHeight = size.height
    }

    question = events[indexPath.row].location
    size = CGSizeMake(screenWidth * 0.3, 9999)
    textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    if cellHeight < size.height {
        cellHeight = size.height
    }

    question = events[indexPath.row].date
    size = CGSizeMake(screenWidth * 0.1, 9999)
    textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    if cellHeight < size.height {
        cellHeight = size.height
    }

    question = events[indexPath.row].time
    size = CGSizeMake(screenWidth * 0.1, 9999)
    textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    if cellHeight < size.height {
        cellHeight = size.height
    }

    return cellHeight
}    

我的数据类有5个标签,我试图找出最大标签的高度。也许你可以告诉我更好的方法,因为我是新手(。然后在我的customTableViewCell.xib中,我的collectionView带有对所有边缘的约束。这里是cellForItemAtIndexPath的代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell:CustomCollectionViewCell? = collectionView.dequeueReusableCellWithReuseIdentifier("FOLDER_CELL", forIndexPath: indexPath) as? CustomCollectionViewCell

    var rect: CGRect?
    switch indexPath.row {
    case 0:
        cell!.frame = CGRectMake(0, 0, self.foldersCollectionView.frame.width * 0.2, self.foldersCollectionView.frame.height)
        rect = cell!.frame
        cell!.label.frame = rect!
        cell!.label.text = event!.category
        break
    case 1:
        cell!.frame = CGRectMake(self.foldersCollectionView.frame.width * 0.2, 0, self.foldersCollectionView.frame.width * 0.3, self.foldersCollectionView.frame.height)
        rect = cell!.frame
        cell!.label.frame = rect!
        cell!.label.text = event!.event
        break
    case 2:
        cell!.frame = CGRectMake(self.foldersCollectionView.frame.width * 0.5, 0, self.foldersCollectionView.frame.width * 0.26, self.foldersCollectionView.frame.height)
        rect = cell!.frame
        cell!.label.frame = rect!
        cell!.label.text = event!.location
        break
    case 3:
        cell!.frame = CGRectMake(self.foldersCollectionView.frame.width * 0.76, 0, self.foldersCollectionView.frame.width * 0.12, self.foldersCollectionView.frame.height)
        rect = cell!.frame
        cell!.label.frame = rect!
        cell!.label.text = event!.date
        break
    case 4:
        cell!.frame = CGRectMake(self.foldersCollectionView.frame.width * 0.88, 0, self.foldersCollectionView.frame.width * 0.12, self.foldersCollectionView.frame.height)
        rect = cell!.frame
        cell!.label.frame = rect!
        cell!.label.text = event!.time
        break
    default:
        break
    }
    println("cell frame - \(cell!.frame)")
    println("cell label frame - \(cell!.label.frame)")
    cell!.layer.borderColor = UIColor.blackColor().CGColor
    cell!.layer.borderWidth = 0.5

    return cell!
}

当我启动我的应用程序时,所有looks都很好但是当我开始滚动tableView时,它看起来像this截图。我不知道如何解决这个问题。也许我必须以不同的方式计算标签的大小,因为在控制台中它总是写下警告:

  

未定义UICollectionViewFlowLayout的行为,因为:   项目高度必须小于UICollectionView的高度减去该部分的顶部和底部值。

有人可以帮我解决我的问题吗?

UPDATE -------------------------------

我找到了另一种计算文字高度的解决方案:

func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
    let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.ByWordWrapping
    label.font = font
    label.textAlignment = .Center
    label.text = text
    label.sizeToFit()
    return label.frame.height
}

以下是我如何使用它:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let screenWidth: CGFloat = UIScreen.mainScreen().bounds.width - 14
    var large: String = events[indexPath.row].category
    var largeWidth: CGFloat = screenWidth * 0.2
    var cellHeight: CGFloat = 0.0
    if count(large) < count(events[indexPath.row].event){
        large = events[indexPath.row].event
        largeWidth = screenWidth * 0.3
    }
    if count(large) < count(events[indexPath.row].location){
        large = events[indexPath.row].location
        largeWidth = screenWidth * 0.26
    }
    if count(large) < count(events[indexPath.row].date){
        large = events[indexPath.row].date
        largeWidth = screenWidth * 0.12
    }
    if count(large) < count(events[indexPath.row].time){
        large = events[indexPath.row].time
        largeWidth = screenWidth * 0.12
    }
    cellHeight = heightForView(large, font: UIFont(name: "HelveticaNeue-Light", size: 18.0)!, width: largeWidth)

    cellHeight += 15.0
    println("ch - \(cellHeight)")
    return cellHeight
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell:CustomCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("FOLDER_CELL", forIndexPath: indexPath) as! CustomCollectionViewCell

    let screenWidth: CGFloat = self.foldersCollectionView.frame.width
    var large: String = event!.category
    var largeWidth: CGFloat = screenWidth * 0.2
    var cellHeight: CGFloat = 0.0
    if count(large) < count(event!.event){
        large = event!.event
        largeWidth = screenWidth * 0.3
    }
    if count(large) < count(event!.location){
        large = event!.location
        largeWidth = screenWidth * 0.26
    }
    if count(large) < count(event!.date){
        large = event!.date
        largeWidth = screenWidth * 0.12
    }
    if count(large) < count(event!.time){
        large = event!.time
        largeWidth = screenWidth * 0.12
    }
    cellHeight = heightForView(large, font: UIFont(name: "HelveticaNeue-Light", size: 18.0)!, width: largeWidth)

    cellHeight += 15.0
    println("fcv - \(cellHeight)")

    var rect: CGRect?
    switch indexPath.row {
    case 0:
        cell.frame = CGRectMake(0, 0, screenWidth * 0.2, cellHeight)
        rect = cell.frame
        //cell.label.frame = rect!
        cell.label.text = event!.category
        break
    case 1:
        cell.frame = CGRectMake(screenWidth * 0.2, 0, screenWidth * 0.3, cellHeight)
        rect = cell.frame
        //cell.label.frame = rect!
        cell.label.text = event!.event
        break
    case 2:
        cell.frame = CGRectMake(screenWidth * 0.5, 0, screenWidth * 0.26, cellHeight)
        rect = cell.frame
        //cell.label.frame = rect!
        cell.label.text = event!.location
        break
    case 3:
        cell.frame = CGRectMake(screenWidth * 0.76, 0, screenWidth * 0.12, cellHeight)
        rect = cell.frame
        //cell.label.frame = rect!
        cell.label.text = event!.date
        break
    case 4:
        cell.frame = CGRectMake(screenWidth * 0.88, 0, screenWidth * 0.12, cellHeight)
        rect = cell.frame
        //cell.label.frame = rect!
        cell.label.text = event!.time
        break
    default:
        break
    }
    cell.layer.borderColor = UIColor.blackColor().CGColor
    cell.layer.borderWidth = 0.5

    return cell
}

现在一切正常,但滚动时tableView有点滞后。你能帮我优化计算吗?

0 个答案:

没有答案