我正在尝试访问我创建的uitablecell中的文本,然后将该文本添加到数据库中。每个单元格旁边都有一个按钮,当用户点击按钮时,它应该将单元格标签文本添加到数据库中,但我无法弄清楚如何。我将不胜感激任何帮助。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellidentifier, forIndexPath: indexPath) as! UITableViewCell
let cellDataParse: PFObject = self.dataparse.objectAtIndex(indexPath.row) as! PFObject
cell.textLabel?.text = cellDataParse.objectForKey("GroupName")! as? String
let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
button.frame = CGRect(x: 250, y: 8, width: 46, height: 30)
button.setTitle("Join", forState: .Normal)
button.backgroundColor = UIColor.redColor()
button.titleLabel?.textColor = UIColor.yellowColor()
cell.addSubview(button)
button.addTarget(self, action: "JoinGroupAction", forControlEvents: UIControlEvents.TouchUpInside)
func JoinGroupAction () {
var usergroups = PFObject(className: "UserGroups")
usergroups["Groups"] = cell.textLabel!.text!
usergroups.save()
var usergrouprelations: PFRelation = PFUser.currentUser()!.relationForKey("MyGroups")
usergrouprelations.addObject(usergroups)
PFUser.currentUser()?.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil {
println("Completed")
} else {
println("Not Completed")
}
})
}
return cell
}
答案 0 :(得分:2)
你可以创建一个UBButton的子类,其中包含一个属性来存储文本的值,然后将其从发送者处取出来进行操作..
class MyButton : UIButton {
var myText : String
}
将按钮的操作从单元格中取出并将其添加为类方法。
然后确保在表格中创建这个新类,并在单元格中将文本标签文本分配给按钮的myText属性。
func myButtonTouchedAction(sender: MyButton) {
let textFromCell = sender.myText
}
还要确保在单元格中定义按钮的操作结尾添加“:”,否则您将无法在函数中使用sender。
正如我在写这篇文章时指出的那样,你可以使用indexPath,虽然我认为它更有用,你可以使用UIButton的默认标记属性来存储当前的indexPath(如果你使用的话,认为它不会起作用多个表格部分,除非您的按钮仅在一个部分中需要),然后根据按钮标签中发送的值重新获取数据。
答案 1 :(得分:1)
作为一般规则,使用视图来保存数据是一个坏主意。您应该创建一个数组来保存单元格数据,将数据保存到该数组中,然后在用户点击按钮时从数组中获取值。
你在做什么部分?您的JoinGroupAction
方法应该是当用户点击表格视图中的按钮时调用的方法吗?怎么连接?它是否作为IBAction连接?如果是这样,它应该有IBAction标签。您还应该使用带有发送者参数的IBAction形式。
答案 2 :(得分:0)
两件事。一,我不会将JoinGroupAction
func放在cellForRowAtIndexPath()
内。其次,您需要做的是稍微更改一下这个函数,以便它获取按钮被点击的indexPath
处的单元格,然后您就可以将文本信息从单元格中拉出来将其上传到Parse。
假设你的按钮有IBAction
,你应该这样做:
func buttonPressed(sender: UIButton) {
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint) as? NSIndexPath {
let cell = cellForRowAtIndexPath(hitIndex)
let parseText = cell.textLabel?.text
//upload your info to Parse
}
}
或者您可以使用objectAtIndexPath
,因为您正在使用Parse。
func buttonPressed(sender: UIButton) {
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint) as? NSIndexPath {
let object = objectAtIndexPath(hitIndex)
let parseText = object.valueForKey("desiredText")
//upload your info to Parse
}
}
答案 3 :(得分:0)
试试这个,
将发件人添加到JoinGroupAction,
SELECT refno, Tatcalculate(to_timestamp((SELECT to_char(h_modified_date,'DD/MM/YYYY HH24:MI:SS') FROM TPADETAILS WHERE refno='WOC0021946'),'DD/MM/YYYY HH24:MI:SS'))
FROM Table1;
然后在JoinGroupAction中添加此内容以获取单元格文本
func JoinGroupAction (sender: UIButton) {
var usergroups = PFObject(className: "UserGroups")
usergroups["Groups"] = cell.textLabel!.text!
usergroups.save()
var usergrouprelations: PFRelation = PFUser.currentUser()!.relationForKey("MyGroups")
usergrouprelations.addObject(usergroups)
PFUser.currentUser()?.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil {
println("Completed")
} else {
println("Not Completed")
}
})
}