有没有办法在UITableViewCell中找到UITextView的高度

时间:2015-11-20 16:47:32

标签: ios uitableview uitextview

我调整大小以适应嵌入在UITableViewCells中的UITexViews,但我想使单元格的高度取决于文本的高度。有没有办法找到textview的高度? 我的应用程序是Universal,需要处理垂直和水平iPad布局。

更新

我不确定您希望看到哪些代码,但这里有一些摘录: 此代码计算行高,但无法访问单元格内容

func calculateSwitchesHeight (viewName: String, text:String) -> CGFloat {

let fetchRequest = NSFetchRequest(entityName: "View")
let viewPredicate = NSPredicate(format: "(viewName == %@)",viewName)
fetchRequest.predicate = viewPredicate

if text == "" {
    return 0.00
}
if text.rangeOfString("*") != nil{ //titles
    if UIDevice.currentDevice().userInterfaceIdiom == .Pad
    {
        return 60 // iPad
    } else { // iPhone
        return 40 // iPhone
    }
}
let charCount: CGFloat = CGFloat(text.characters.count)
NSLog("charcount = > %f", charCount)
if UIDevice.currentDevice().userInterfaceIdiom == .Pad
{
    switch charCount {
    case 0 ... 89: return 100
    default: return charCount + 10
    }

} else { // iPhone
    switch charCount {
    case 0 ... 69: return 70
    case 300 ... 350: return 240 // good for 343
    default: return charCount + 00
    }
 }

} // end calculateSwitchesHeight

以下是我如何调用它:

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        // calculate length of text and adjust the height of row for that
        return calculateSwitchesHeight(viewName, text:getSwitchInfo(viewName, switchOrder: indexPath.row, sectionNumber: indexPath.section).switchItemText)
    }

我使用此函数获取文本

func getSwitchInfo (viewName:String, switchOrder:Int, sectionNumber: Int) ->(switchItemText: String, switchType: String, switchItemSetting: Bool) {

let fetchRequest = NSFetchRequest(entityName: "Switch")
let viewPredicate = NSPredicate(format: "(viewName == %@)",viewName)
let rowPredicate = NSPredicate(format: "(switchOrder == %i)",switchOrder)
var switchItemText = ""
var switchType = ""
var switchItemSetting:Bool = false
var switchValue:NSNumber = 0

fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates:[viewPredicate, rowPredicate])
// Execute the fetch request
do {
    let fetchResults = try managedObjectContext!.executeFetchRequest(fetchRequest) as! [Switch]
    let appConfig = fetchResults[0]
    switchItemText  = appConfig.switchDescription!
    switchValue  = appConfig.switchValue!
    switchType  = appConfig.switchType!
    if switchValue == 1 {
        switchItemSetting = true
     }

} catch let error as NSError {
    // failure
    print("Fetch failed: \(error.localizedDescription)")
} // end fetch results
return (switchItemText, switchType, switchItemSetting)
}

0 个答案:

没有答案