首页 \ 问答 \ 快速使用未解析的标识符'IndexPath'TableView单元格按钮(Swift Use of Unresolved Identifier 'IndexPath' TableView Cell Button)

快速使用未解析的标识符'IndexPath'TableView单元格按钮(Swift Use of Unresolved Identifier 'IndexPath' TableView Cell Button)

我试图获取我的UILabel的文本并将其设置为我的Parse对象,但我遇到了一个问题,将对象设置为单元格的索引路径。 我在该行Use of unresolved identifier 'indexPath'错误。

follow["following"] = self.userArray.objectAtIndex(IndexPath.row)

这是我的tableview控制器:

import UIKit

class SearchUsersRegistrationTableViewController: UITableViewController {

    var userArray : NSMutableArray = []

    override func viewDidLoad() {
        super.viewDidLoad()

        var user = PFUser.currentUser()

        loadParseData()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return userArray.count

    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell: SearchUsersRegistrationTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SearchUsersRegistrationTableViewCell

        let row = indexPath.row

        var individualUser = userArray[row] as! PFUser
        var username = individualUser.username as String

        var profileImage = individualUser["profileImage"] as? PFFile

        if profileImage != nil {

        profileImage!.getDataInBackgroundWithBlock({
            (result, error) in

            cell.userImage.image = UIImage(data: result)

        })
        } else {
            cell.userImage.image = UIImage(named: "profileImagePlaceHolder")

        }

        cell.usernameLabel.text = username

        cell.addUserButton.tag = row

        cell.addUserButton.addTarget(self, action: "addUser:", forControlEvents: .TouchUpInside)

        return cell

    }

    func loadParseData() {

        var query : PFQuery = PFUser.query()

        query.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!) -> Void in

            if error == nil {

                if let objects = objects {

                    println("\(objects.count) users are listed")

                    for object in objects {

                        self.userArray.addObject(object)

                    }
                    self.tableView.reloadData()
                }
            } else {
                println("There is an error")
            }
        }
    }

    @IBAction func addUser(sender: UIButton) {

        println("Button Triggered")

        let addUserButton : UIButton = sender

        let user : PFObject = self.userArray.objectAtIndex(addUserButton.tag) as! PFObject



        var follow = PFObject(className: "Follow")


        follow["following"] = self.userArray.objectAtIndex(IndexPath.row)
        follow["follower"] = PFUser.currentUser().username


        follow.saveInBackground()

    }

}

这是我的tableview单元格:

import UIKit

class SearchUsersRegistrationTableViewCell: UITableViewCell {



    @IBOutlet weak var userImage: UIImageView!
    @IBOutlet weak var usernameLabel: UILabel!
    @IBOutlet weak var addUserButton: UIButton!


    override func awakeFromNib() {
        super.awakeFromNib()

        userImage.layer.borderWidth = 1
        userImage.layer.masksToBounds = false
        userImage.layer.borderColor = UIColor.whiteColor().CGColor
        userImage.layer.cornerRadius = userImage.frame.width/2
        userImage.clipsToBounds = true


    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }



}

I am attempting to get the text of my UILabel and set it to my Parse object, but I am running into an issue setting the object to the index path of the cell. I am getting an Use of unresolved identifier 'indexPath' error at that line.

follow["following"] = self.userArray.objectAtIndex(IndexPath.row)

Here is my tableview controller:

import UIKit

class SearchUsersRegistrationTableViewController: UITableViewController {

    var userArray : NSMutableArray = []

    override func viewDidLoad() {
        super.viewDidLoad()

        var user = PFUser.currentUser()

        loadParseData()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return userArray.count

    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell: SearchUsersRegistrationTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SearchUsersRegistrationTableViewCell

        let row = indexPath.row

        var individualUser = userArray[row] as! PFUser
        var username = individualUser.username as String

        var profileImage = individualUser["profileImage"] as? PFFile

        if profileImage != nil {

        profileImage!.getDataInBackgroundWithBlock({
            (result, error) in

            cell.userImage.image = UIImage(data: result)

        })
        } else {
            cell.userImage.image = UIImage(named: "profileImagePlaceHolder")

        }

        cell.usernameLabel.text = username

        cell.addUserButton.tag = row

        cell.addUserButton.addTarget(self, action: "addUser:", forControlEvents: .TouchUpInside)

        return cell

    }

    func loadParseData() {

        var query : PFQuery = PFUser.query()

        query.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!) -> Void in

            if error == nil {

                if let objects = objects {

                    println("\(objects.count) users are listed")

                    for object in objects {

                        self.userArray.addObject(object)

                    }
                    self.tableView.reloadData()
                }
            } else {
                println("There is an error")
            }
        }
    }

    @IBAction func addUser(sender: UIButton) {

        println("Button Triggered")

        let addUserButton : UIButton = sender

        let user : PFObject = self.userArray.objectAtIndex(addUserButton.tag) as! PFObject



        var follow = PFObject(className: "Follow")


        follow["following"] = self.userArray.objectAtIndex(IndexPath.row)
        follow["follower"] = PFUser.currentUser().username


        follow.saveInBackground()

    }

}

Here is my tableview cell:

import UIKit

class SearchUsersRegistrationTableViewCell: UITableViewCell {



    @IBOutlet weak var userImage: UIImageView!
    @IBOutlet weak var usernameLabel: UILabel!
    @IBOutlet weak var addUserButton: UIButton!


    override func awakeFromNib() {
        super.awakeFromNib()

        userImage.layer.borderWidth = 1
        userImage.layer.masksToBounds = false
        userImage.layer.borderColor = UIColor.whiteColor().CGColor
        userImage.layer.cornerRadius = userImage.frame.width/2
        userImage.clipsToBounds = true


    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }



}

原文:https://stackoverflow.com/questions/30078740
更新时间:2021-06-07 17:06

最满意答案

尝试这个

follow["following"] = self.userArray.objectAtIndex(sender.tag)

您将行设置为按钮的标记。 只是使用它。


Try this

follow["following"] = self.userArray.objectAtIndex(sender.tag)

You are setting the row as tag for your button. Just use it.

相关问答

更多

错误:使用未解析的标识符'indexPath'(Error: Use of unresolved identifier 'indexPath')

只需在cellforRowAtIndexPath中为您的按钮指定一个标记,该标记将等于indexPath.row someButton.tag = indexPath.row 现在你的func应该是: IBAction func share(sender: UIButton) { let defaultText = "Check out my Pinpoint: " + self.pinpoints[sender.tag].main + ". Get Pinpoint on the i ...

静态tableview单元格内的动态Tableview(Dynamic Tableview inside a Static tableview Cell)

经过对这一次的惊人努力之后,我找到了解决方案。 关于以下内容: Swift:Static UITableViewCell中的TableView 解决问题的地方说: 通过试验这个我可以确定,你不能使用相同的UITableViewController作为两个表视图的数据源和委托。 使用静态表视图,您根本不应该实现数据源方法。 奇怪的是,即使我断开数据源并委托我的静态表视图和表视图控制器之间的连接,该表视图仍然在我的表视图控制器类中调用numberOfRowsInSection。 如果我在代码中将数据源 ...

如何知道单元格SWIFT的indexPath [重复](How to know the indexPath of the cell SWIFT [duplicate])

下面的代码将为您提供Button标签,并且还知道点击了哪个按钮。 在cellForRowAtIndexPath UITableView DataSource Delegate方法中使用此代码。 cell.btn.tag = indexPath.row cell.btn.addTarget(self, action: #selector(btnTapped(_:)), for: .touchUpInside) func btnTapped(_ sender : UIButt ...

快速使用未解析的标识符'IndexPath'TableView单元格按钮(Swift Use of Unresolved Identifier 'IndexPath' TableView Cell Button)

尝试这个 follow["following"] = self.userArray.objectAtIndex(sender.tag) 您将行设置为按钮的标记。 只是使用它。 Try this follow["following"] = self.userArray.objectAtIndex(sender.tag) You are setting the row as tag for your button. Just use it.

SWIFT:2在tableview中定制单元格。(SWIFT: 2 custom cell in tableview. What's wrong?)

您需要在外部绘制单元格声明: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell: CustomCell! if indexPath.row == 0{ cell = tableView.dequeueReusableCellWithIdentifier("FistCustomTableViewCell" ...

Swift中未解析的标识符[关闭](unresolved identifier in Swift [closed])

在cellForRowAtIndexPath方法中,你必须用它的storyboard id标识你的tableViewCell,在这种情况下它是Cell override func viewDidLoad() { tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "Cell") } func tableView(tableView: UITableView, cellForRowAtInde ...

错误:在prepareForSeque函数中使用未解析的标识符“indexPath”(Error: Use the unresolved identifier 'indexPath' in prepareForSeque function)

更改 if indexPath = self.tableView.indexPathForSelectedRow! { } 如果使用swift低于2.0,因为indexPathForSelectedRow不是一个属性,它的方法是这样的use(),并返回可选,所以你需要使用let if let indexPath = self.tableView.indexPathForSelectedRow() { } 截图来自Xcode 6.4 对于swift 2.0,最后不要使用()作为它的属性 if ...

使用未解析的“自我”标识符(Use of unresolved identifier for “self”)

正确使用PFQueryTableViewController import UIKit import ParseUI class WaitListTVC: PFQueryTableViewController { override init(style: UITableViewStyle, className: String?) { super.init(style: style, className: className) } required init(coder aDecode ...

获取indexPath之后但在删除该indexPath上的单元格之前调用reloadData()是不安全的吗?(Is it unsafe to call reloadData() after getting an indexPath but before removing a cell at that indexPath?)

是的,可能正在发生的是表视图使存储的索引路径无效。 要测试是否存在问题,请尝试在调用reloadData()之前更改表中表示的数据。 如果这是一个问题,那么您将需要使用表格单元格表示的对象的标识符而不是索引路径。 修改后的代码如下所示: func objectIdentifer(at: IndexPath) -> Identifier? { ... } func indexPathForObject(_ identifier: Identifier) -> IndexPath? { ...

iOS 8,Swift indexPath.row错误“未解析的标识符”(iOS 8, Swift indexPath.row error “unresolved identifier”)

问题在于您的功能定义。 您错过了在那里指定参数名称。 更改: override func tableView(tableView: UITableView, didSelectRowAtIndexPath: NSIndexPath) 至 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) The issue is with your function ...

相关文章

更多

最新问答

更多
  • 如何从远程文件拉取文件而不覆盖本地文件?(How do I pull files from remote without overwriting local files?)
  • Reactjs:状态改变时重新渲染iframe(Reactjs: re-renders iframes when state changed)
  • 奇怪的网址,以及跟随php页面流程的困难(odd url, and difficulty in following the php page flow)
  • 标签活动无效(Tab Activity is not working)
  • JavaME合适的语法编译器建议?(JavaME-suitable grammar compiler recommendations?)
  • 指定参数(Specifying arguments)
  • 可以通过Ruby插件或控制台覆盖Sketchup中的键盘快捷键吗?(Can one override keyboard shortcuts in Sketchup through the a Ruby Plugin or Console?)
  • 计算Java EE Web App中用户数的最佳方法(Best way to count number of users in a Java EE web App)
  • 无法使用templateUrl加载cordova中的外部模板(unable to load external templates in cordova with templateUrl)
  • PHPExcel:写入期间无法使用缓存(PHPExcel: Unable to use cache during write)
  • 在javascript中嵌套这个指针(nested this pointer in javascript)
  • 谁跟领航致远培训过,有问题问下啊
  • 控制器要求在入门时下载(Controller ask to download on entry)
  • 未能通过conda安装Asyncio(Failure to install Asyncio via conda)
  • 如何查找已完成项目的总长度?(How to find length of total completed items?)
  • 如何检查OleInitialize是否已被调用?(How to check if OleInitialize has already been called?)
  • SQL在特定范围内返回列中具有最大值的行(SQL Returning rows with max value in column, within a specific range)
  • preg_match从url获取id(preg_match get the id from url)
  • 如何在运算符中为make方程转换perl变量?(How to convert a perl variable in a operator for make equations?)
  • 在导航上方添加空格/标题。(Add a white space/ header above navigation.)
  • MeetingItem已保存;(MeetingItem saved; but change now shown in Calendar)
  • c#vb:我们应该使用System.Lazy进行资源密集型任务吗?(c# vb: Should we use System.Lazy for resource-intensive task? (when threading is not needed))
  • 为什么在armeabi代码中使用armeabi-v7a代码?(Why use armeabi-v7a code over armeabi code?)
  • 获取请求的自定义标头(Java HTTP)(Fetching a custom header of a request (Java HTTP))
  • 是否可以在嵌套的if语句中从varchar转换为numeric以动态评估参数?(Is it possible to convert from varchar to numeric within a nested if statement in order to dynamically evaluate a parameter?)
  • 如何将Html.ActionLink转换为链接到Ajax调用的按钮?(How to convert from Html.ActionLink to a button linked to Ajax call?)
  • 应用程序如何处理Windows符号链接?(How are Windows symbolic links treated by the apps?)
  • html,js,css在jsfiddle中工作,但不在sharepoint中(html, js, css works in jsfiddle but not in sharepoint)
  • 从Ruby脚本调用Elasticsearch Rest API(Calling Elasticsearch Rest API from Ruby script)
  • 如何将嵌套setTimeouts转换为承诺(How to convert nested setTimeouts to promises)