I have an app which have a table view
and iam using custom cell class in my table
and this is my custom table cell class :
class addCommentTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
postText.layer.borderWidth = 1
postText.layer.borderColor = UIColor.grayColor().CGColor
postText.layer.cornerRadius = 3
}
@IBOutlet weak var postText: UITextView!
@IBOutlet weak var postCommentButton: UIButton!
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
i need to click the button in the cell class and get the value from the textView
so i added this code to make that :
class singleNewViewController: UIViewController , UITableViewDataSource , UITableViewDelegate{
override func viewDidLoad() {
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
let secondCellPostComment = mainTable.dequeueReusableCellWithIdentifier("addCommentCell" , forIndexPath: indexPath) as! addCommentTableViewCell
let postButton = secondCellPostComment.postCommentButton
postButton!.addTarget(self, action: "postUserComment", forControlEvents: .TouchUpInside)
}
func postUserComment()
{
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
let secondCellPostComment = mainTable.dequeueReusableCellWithIdentifier("addCommentCell" , forIndexPath: indexPath) as! addCommentTableViewCell
var textFieldComment = secondCellPostComment.postText
let textF = textFieldComment.text!
let request = NSMutableURLRequest(URL: NSURL(string: "http://mydomain/comment")!)
request.HTTPMethod = "POST"
let postString = "user_id=(addResourcesViewController.userLoggedID)&news_id=(singleNewViewController.newID)&comment=(textF.text!)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data , response , error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is (httpStatus.statusCode)")
print("response = (response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
dispatch_async(dispatch_get_main_queue(), {
self.mainTable.reloadData()
})
}
task.resume()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (indexPath.row == 0 )
{
let cell = mainTable.dequeueReusableCellWithIdentifier("singleTableDetails", forIndexPath: indexPath) as! singleNewTableViewCell
let readmorebutton = cell.readmore
let focusComment = cell.addcomments
readmorebutton.addTarget(self, action: "loadURLinBrowser", forControlEvents: UIControlEvents.TouchUpInside)
focusComment.addTarget(self, action: "focusToView", forControlEvents: UIControlEvents.TouchUpInside)
let remoteImageUrlString = "http://mydomian/"+singleNewViewController.image
let imageURL = NSURL(string: remoteImageUrlString)
let myBlock: SDWebImageCompletionBlock! = { (image:UIImage! , error : NSError! , cacheType: SDImageCacheType! , imageURL: NSURL! ) -> Void in
print("Image with urk (imageURL.absoluteString) is loaded" )
}
cell.cellImage.sd_setImageWithURL(imageURL, placeholderImage: UIImage(named:"welcome_bg") , options: SDWebImageOptions.ProgressiveDownload, completed: myBlock)
return cell
}else if (indexPath.row == 1 )
{
let cell = mainTable.dequeueReusableCellWithIdentifier("addCommentCell", forIndexPath: indexPath) as! addCommentTableViewCell
return cell
}
else
{
let cell = mainTable.dequeueReusableCellWithIdentifier("allCommentsCell", forIndexPath: indexPath) as! commentsTableViewCell
cell.commentUser.text = self.commentsUser[(indexPath.row - 2)]
cell.comment.text = self.comments[(indexPath.row - 2)]
return cell
}
}
}
the code is working but i don't get the value of the UITextView
and i tried to add a default value to the UITextView from the storyboard it's working but when i run in simulator and replace it with any string it's send the default value to the url any suggestion why it's not read any new values ?
Aucun commentaire:
Enregistrer un commentaire