我想从自定义UITableViewCell中的按钮执行segue,但是当我按下该按钮时,我不知道如何访问单元格的内容.我意识到这可以通过didSelectRowAtIndexPath来完成,但是我有那个方法执行另一个函数,我想使用我在表格单元格中创建的按钮.
这是我的执行segue方法我遇到了麻烦:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "showComments"
{
let vc:CommentOnPostViewController = segue.destinationViewController as CommentOnPostViewController
var buttonPosition:CGPoint = sender?.convertPoint(CGPointZero, toView: self.feed) as CGPoint!
var path:NSIndexPath = self.feed.indexPathForRowAtPoint(buttonPosition) as NSIndexPath!
var postToCommentOn:PFObject = feedList[path.row] as PFObject
vc.post = postToCommentOn as PFObject
}
}
Run Code Online (Sandbox Code Playgroud)
我在显示单元格时标记按钮,并给它一个目标动作:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
// There is code that goes here for creating and displaying the cell.
cell.commentButton.tag = indexPath.row
cell.commentButton.addTarget(self, action: "addComment", forControlEvents: UIControlEvents.TouchUpInside)
}
Run Code Online (Sandbox Code Playgroud)
以下是按下按钮时调用的操作: …
我收到Cannot form weak reference to instance (0x15919e00) of class TestClass. It is possible that this object was over-released, or is in the process of deallocation.以下代码位的错误。
如果我删除addObserver和removeObserver行,我的代码不会崩溃。我不确定为什么会这样。有没有人有任何想法?在此先感谢您的帮助!
class TestClass: NSObject {
lazy var tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 52.0
tableView.delegate = self
tableView.dataSource = self
let nib = UINib(nibName: "CustomCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "customCell")
tableView.addObserver(self, forKeyPath: "myPath", options:nil, context: nil)
return tableView
}() …Run Code Online (Sandbox Code Playgroud) 如何在不中断循环的情况下在循环中执行多个guard语句?如果一个保护语句失败,它会将我踢出当前循环迭代并绕过剩余的代码.
for user in users {
guard let first = user["firstName"] as? String else {
print("first name has not been set")
continue
}
print(first)
guard let last = user["lastName"] as? String else {
print("last name has not been set")
continue
}
print(last)
guard let numbers = user["phoneNumbers"] as? NSArray else {
print("no phone numbers were found")
continue
}
print(numbers)
}
Run Code Online (Sandbox Code Playgroud)
如何确保为每个用户执行所有语句?在else块中放置return和break也不起作用.谢谢!
cellForRowAtIndexPath当我尝试创建自定义表格单元格时,我的程序在方法中不断崩溃。我在代码的每一行都放置了一个断点,在我尝试创建带有错误 的单元格变量后代码失败:unexpectedly found nil while unwrapping optional value。我以前曾多次成功使用自定义表格单元格,但从未遇到过此问题。这看起来很简单,但我无法弄清楚出了什么问题。也许 xcode 中的某些内容发生了我不知道的变化?任何帮助表示赞赏。谢谢!...此外,我确实在viewDidLoad. 我也会包含该代码。
var nib = UINib(nibName: "FeedTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "feedCell")
Run Code Online (Sandbox Code Playgroud)
问题代码如下:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:FeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("feedCell", forIndexPath: indexPath) as! FeedTableViewCell
cell.descriptionLabel.text = "Testing the description label of my cell."
return cell
}
Run Code Online (Sandbox Code Playgroud)
更新
这个项目和其他已经成功的项目之间的一个区别是 Xcode 现在提示我在 as 后面放一个 bang (!),而之前我从未使用过。如果我不添加 (!) 它会给我错误消息“AnyObject 不可转换为FeedTableViewCell”
作为!FeedTableViewCell- 而不是 - 作为FeedTableViewCell
FeedTableViewCell …