ios - 使用 let buttonPosition = sender.convertPoint(CGPointZero, toView : self. tableView) 点击时更改 TableViewCell 中 UIButton 的标题

标签 ios swift uitableview uibutton

在这里,我从 Parse 检索了数据,并在 tableViewCell 中显示该数据。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("users", forIndexPath: indexPath) as! userListTableViewCell

    let userData:PFObject = self.data.objectAtIndex(indexPath.row) as! PFObject

    // Usernames and gender..

    myCell.fullName.text = userData.objectForKey("fullName") as! String!
    myCell.genderLabel.text = userData.objectForKey("gender") as! String!

    // Profile pictures..

    let profilePics = userData.objectForKey("profilePicture") as! PFFile
    profilePics.getDataInBackgroundWithBlock { (data, error) -> Void in

        if let downloadedImage = UIImage(data: data!) {

            myCell.dp.image = downloadedImage
        }

    }

    myCell.followButtton.addTarget(self, action: "followButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)



    return myCell
}

现在我已经申请了UIButtonUITableViewCell成功了,我通过这种方式很好地获得了坐标:

// IBActions..


func followButtonTapped(sender:AnyObject) {
    let buttonPosition = sender.convertPoint(CGPointZero, toView: self.tableView)
    let indexPath = self.tableView.indexPathForRowAtPoint(buttonPosition)
    if indexPath != nil {



        println(indexPath)


    }
}

现在,当用户点击任何按钮时,我想更改该按钮的标题。感谢您抽出时间......如果这是我问的基本问题,我很抱歉,我是初学者。

这就是点击按钮时我得到的结果..

<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0} <NSIndexPath: 0xc000000000010016> {length = 2, path = 0 - 2}

我可以使用这个吗?

func followButtonTapped(sender:AnyObject) {
let buttonPosition = sender.convertPoint(CGPointZero, toView: self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(buttonPosition)
if indexPath != nil {

    sender.setTitle("title", forState: UIControlState.Normal)

    println(indexPath)


   }
}

..我愿意接受任何类型的答案..任何更好的方法也会被接受..

最佳答案

func followButtonTapped(sender:AnyObject) {
    let buttonPosition = sender.convertPoint(CGPointZero, toView: self.tableView)
    let indexPath = self.tableView.indexPathForRowAtPoint(buttonPosition)
    if indexPath != nil {

        if let cell = tableView.cellForRowatindexPath(indexPath){
       cell.followButton.setTitle("input title here")
}


        println(indexPath)


    }
}

关于ios - 使用 let buttonPosition = sender.convertPoint(CGPointZero, toView : self. tableView) 点击时更改 TableViewCell 中 UIButton 的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31897107/

相关文章:

ios - 我的 Imageview 未在 iPad 中显示?

ios - 为 appstore 提交版本控制 ios 应用程序

ios - 文本字段的平均值取决于文本字段的数量 "used"

ios - AudioKit 4.3 : record audio, 离线渲染,然后播放

ios - 同时以相同的速度将多个连续的 UILabels 动画到同一点

ios - 根据字符集的任意字符分割字符串

ios - 在后台模式下关闭我的应用程序后将下载简历

ios - 在不重新加载单元格的情况下在 UITableViewCell 上设置编辑样式

ios - 如何从 TableSource 修改 TableViewCell 内的 UIButton?

ios - 如何给 UITableView 底部空白区域染色?