我有一个带有一些标签,一个滑块和一个播放按钮的自定义TableViewCell。如果没有音频文件,则该单元不可扩展,并且滑块和播放按钮不可见。但是,当有音频时,该单元会展开以显示所有内容。
如果我进入编辑模式并按减号,它将按预期工作。
现在解决问题。单元格滑过以显示删除按钮。如果我展开和收缩单元格,则按减号,它会飞离屏幕。这是一个gif,这样您就可以明白我的意思。
这是我实现的一些方法
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! HistoryTBCell
cell.autoresizingMask = UIViewAutoresizing.FlexibleHeight
cell.clipsToBounds = true
tableView.beginUpdates()
if cell.isExpandable == true {
if toggle == 1 {
selectedRow = indexPath
toggle = 0
} else {
selectedRow = NSIndexPath(forRow: 1, inSection: 2)
toggle = 1
}
}
tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if selectedRow == indexPath {
return 120
} else {
return 60
}
}
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if self.tableView.editing {
return .Delete
} else {
return .None
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! HistoryTBCell
cell.selectionStyle = .None
cell.clipsToBounds = true
let hists = history[indexPath.row]
let date = hists.valueForKey("currentDate") as! String
let speechTime = hists.valueForKey("speechTime") as! String
let actualTime = hists.valueForKey("actualTime") as! String
let rec = hists.valueForKey("recURL") as! String
cell.configure(date: date, speech: "Speech Time: \(speechTime)", actual: "Actual Time: \(actualTime)")
if rec != "" {
cell.setUpAudio(url: rec)
}
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let alert = UIAlertController(title: "Delete", message: "Are you sure you want to delete?\nThe recording will also be deleted", preferredStyle: .ActionSheet)
alert.addAction(UIAlertAction(title: "Delete", style: .Default, handler: { void in
//let context = self.appDelegate?.managedObjectContext
let fileManager = NSFileManager.defaultManager()
let hists = self.history[indexPath.row]
let rec = hists.valueForKey("recURL") as! String
self.getContext().deleteObject(self.history[indexPath.row])
self.history.removeAtIndex(indexPath.row)
do {
try self.getContext().save()
} catch {
print("Could not save")
}
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
do {
let appended = rec
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let pathurl = documentsDirectory.URLByAppendingPathComponent(appended)
try fileManager.removeItemAtPath(pathurl.path!)
} catch {
print("Could not remove item")
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}
}
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
selectedRow = NSIndexPath(forRow: 1, inSection: 2)
}
我猜想它与调整大小有关。如果需要,我可以发布更多代码。谢谢
最佳答案
我要做的就是注释掉这一行:
cell.autoresizingMask = UIViewAutoresizing.FlexibleHeight