不应该添加单元格中的按钮
Button in cell is being added when it is not supposed to be
所以我有一个设置按钮,它只能添加到具有当前用户名的单元格中。但是,它似乎被随机添加到一个单元格中。在我创建按钮的语句中,它只被 创建一次 但是它被添加到多个单元格。我已附上问题图片,ps 当前用户名是 "test",因此设置按钮不应与 matt short 用户位于同一单元格中。谢谢,下面是我正在创建和添加按钮子视图的函数的附加代码。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("chatCell") as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "chatCell")
}
cell!.selectionStyle = UITableViewCellSelectionStyle.None
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
var sectionTitle = self.friendSectionTitles[indexPath.section]
var friendArray: [String]! = friendDict[sectionTitle]
var friend = friendArray[indexPath.row]
if sectionTitle == "me" && friend == PFUser.currentUser().username {
var settingsButton: UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
settingsButton.frame = CGRectMake(screenWidth - 100 , 5, 50, 30)
settingsButton.addTarget(self, action: "settingsButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
settingsButton.setTitle("Settings", forState: UIControlState.Normal)
cell!.addSubview(settingsButton)
}
cell!.textLabel!.text = friend
return cell!
}
In the statement in which I create the button it is only being created once however it is being added to multiple cells
因为单元格一旦创建,就会被 table 的其他行重复使用。如果您不希望按钮出现在重复使用的单元格中,您将需要(在 cellForRowAtIndexPath:
的实现中)检测它的存在 并将其删除 (或至少隐藏它)对于不应该有它的每一行。
所以我有一个设置按钮,它只能添加到具有当前用户名的单元格中。但是,它似乎被随机添加到一个单元格中。在我创建按钮的语句中,它只被 创建一次 但是它被添加到多个单元格。我已附上问题图片,ps 当前用户名是 "test",因此设置按钮不应与 matt short 用户位于同一单元格中。谢谢,下面是我正在创建和添加按钮子视图的函数的附加代码。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("chatCell") as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "chatCell")
}
cell!.selectionStyle = UITableViewCellSelectionStyle.None
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
var sectionTitle = self.friendSectionTitles[indexPath.section]
var friendArray: [String]! = friendDict[sectionTitle]
var friend = friendArray[indexPath.row]
if sectionTitle == "me" && friend == PFUser.currentUser().username {
var settingsButton: UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
settingsButton.frame = CGRectMake(screenWidth - 100 , 5, 50, 30)
settingsButton.addTarget(self, action: "settingsButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
settingsButton.setTitle("Settings", forState: UIControlState.Normal)
cell!.addSubview(settingsButton)
}
cell!.textLabel!.text = friend
return cell!
}
In the statement in which I create the button it is only being created once however it is being added to multiple cells
因为单元格一旦创建,就会被 table 的其他行重复使用。如果您不希望按钮出现在重复使用的单元格中,您将需要(在 cellForRowAtIndexPath:
的实现中)检测它的存在 并将其删除 (或至少隐藏它)对于不应该有它的每一行。