【发布时间】:2016-08-16 00:49:45
【问题描述】:
我重新打开这个问题是因为我的最后一个问题被标记为重复,即使它实际上不是!这是同样的问题,但解决方案不适用于我的代码。我正在使用 swift 2。
所以我的问题是,正如标题所说:我在 tableViewCell 中有一个 UIButton,当我使用方法 «setTitle » 时,更新标题需要 10 到 60 秒。同时我正在使用 « addTarget » 它可以立即运行。所以标题也应该更新。我的按钮在我的故事板中设置为“自定义”。
当视图加载时,我正在运行以下代码:
/* viewDidLoad */
override func viewDidLoad() {
super.viewDidLoad()
boolAlready = false
findParticipation()
}
/* 查找参与 */
func findParticipation() {
// After server request response :
boolAlready = true
self.tableView.reloadData()
}
/* cellForRowAtIndexPath */
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellActions = tableView.dequeueReusableCellWithIdentifier(informationsCellArray[indexPath.row], forIndexPath: indexPath) as ! eventDetailsAction
if boolAlready {
cellActions.foundParticipation
} else {
cellActions.btnParticipate.setTitle("...", forState: UIControlState.Normal)
cellActions.btnParticipate.addTarget(self, action: « deleteParticion", forControlEvents: .TouchUpInside)
}
/* 在我的自定义单元格中 */
func foundParticipation () {
self.btnParticipate.setTitle("Annuler", forState: UIControlState.Normal)
self.btnParticipate.addTarget(self, action: "deleteParticipation", forControlEvents: .TouchUpInside)
}
我在论坛上发现了一些不起作用的东西:
设置我的 settitle 动作
dispatch_async(dispatch_get_main_queue()) {}为所有不同的 UIControlStates 设置标题
使用
setAttributedTitle()在setTitle之后使用
self.btnParticipate.setNeedsLayout()和self.btnParticipate.layoutIfNeeded()在setTitle之前禁用按钮并在setTitle之后启用它
self.addSubview(self.btnParticipate)更改
titleLabel.text中的标题使用
cellActions.btnParticipate在父视图控制器中执行前面所说的所有操作UIView.performWithoutAnimation { self.btnParticipate.setTitle("Annuler", forState: .Normal) }
我现在陷入困境,找不到解决方案。
【问题讨论】:
-
那么,
foundParticipation()方法总是在主线程上调用? -
是的,它只在由
tableView.reloadData触发的cellForRowAtIndexPath中调用。而且你只能在主线程中使用tableView.reloadData。