2016-03-28 6 views
0

Я создаю приложение простой статьи в IOS с помощью Swift. У меня возникают проблемы с обновлением текста внутри моего текста. но у меня есть textView в пользовательском классе TableViewCell и не могу понять, как изменить текст. Я также попытался сделать функцию сеттера. У меня нет журналов ошибок, я печатаю содержимое ячейки после создания и после изменения текста. Когда я его создаю, у него есть текст владельца места, после его изменения он изменяется в ячейке в ячейке forRow, но физически отображается текст из xib.Пользовательский текст Cell Text текст не меняется

import UIKit 

class ViewController2: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextViewDelegate { 

//mydata 
var articles = ["Article","Article","Article","Article","Article","Article","Article"] 
var farmers = ["farmer","farmer","farmer","farmer","farmer","farmer","farmer",] 
var products = ["coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee","coffee",] 
var article = "I am aware that this question has been asked, but none of the answers have worked for me. I'm trying to implement a comments View controller, similar to what you can see in Instagram, where the size of the tableView cell depends on the size of the comment. So I though I would get the necessary height to display the whole comment in textView without scrolling, adjust the textView, then use it to set the heightForRowAtIndexPath appropriately, before finally reloading the table. However, I can't even get to resize the textView, I have tested a certain number of answers and still the textView won't budge." 

//flags 
var flag = 0 //0=article, 1 = categories, 2 = productpage 


// outlets 
@IBOutlet weak var tableView: UITableView! 

///Default 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view, typically from a nib. 
    let nib1 = UINib(nibName: "Picture2", bundle: nil) 
    tableView.registerNib(nib1, forCellReuseIdentifier: "Picture2") 
    let nib2 = UINib(nibName: "Title", bundle: nil) 
    tableView.registerNib(nib2, forCellReuseIdentifier: "Title") 
    let nib3 = UINib(nibName: "Article", bundle: nil) 
    tableView.registerNib(nib3, forCellReuseIdentifier: "Article") 



} 

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



//TableView 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    switch flag 
    { 
    case 0: 
     return 3 
    case 1: 
     return products.count 
    case 2: 
     return farmers.count 
    default: 
     return 1 
    } 
} 
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    switch flag 
    { 
    case 0: 
     if(indexPath.row == 0) 
     { 
      return 216; 
     } 
     else if(indexPath.row == 1) 
     { 
      return 80; 
     } 
     else 
     { 
      var hieght = calculateHeightForString(article) 
      return hieght 
     } 
    case 1: 
     return 44 
    case 2: 
     return 216 
    default: 
     return 216 
    } 
} 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    switch flag 
    { 
    case 0: 
     self.performSegueWithIdentifier("View2", sender: self) 
    case 1: 
     self.performSegueWithIdentifier("View2", sender: self) 
    case 2: 
     //self.performSegueWithIdentifier("Product", sender: self) 
     break 
    default: 
     return self.performSegueWithIdentifier("View2", sender: self) 
    } 

} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    switch flag 
    { 
    case 0: 
     if(indexPath.row == 0) 
     { 
      let cell = self.tableView.dequeueReusableCellWithIdentifier("Picture2", forIndexPath: indexPath) as! Picture2Cell 
      let imageName = "Bag.png" 
      let image = UIImage(named: imageName) 
      cell.Picture.image = image 
      return cell 
     } 
     else if(indexPath.row == 1) 
     { 
      let cell = self.tableView.dequeueReusableCellWithIdentifier("Title", forIndexPath: indexPath) as! TitleCell 
      cell.title.text = "THIS IS THE TTITLE" 
      cell.by.text = "Zach Chandler" 
      cell.country.text = "Camaroon" 
      return cell 
     } 
     else 
     { 
      var cell = self.tableView.dequeueReusableCellWithIdentifier("Article", forIndexPath: indexPath) as! ArticleCell 
      print(cell.textView.text) 
      println("Changed") 
      let currentText:NSString = article 
      cell.textView.text = currentText as String 
      print(cell.textView.text) 
      return cell 
     } 
    case 2: 
     let cell = self.tableView.dequeueReusableCellWithIdentifier("MainCell", forIndexPath: indexPath) as! Picture1Cell 
     cell.title.text = "indexpath.section \(indexPath.section)" 
     let imageName = "Bag.png" 
     let image = UIImage(named: imageName) 
     cell.picture.image = image 
     cell.subtitle.text = "indexPath.row \(indexPath.row)" 
     return cell 
    case 1: 
     let cell = self.tableView.dequeueReusableCellWithIdentifier("ProductCell", forIndexPath: indexPath) as! UITableViewCell 

     cell.textLabel!.text = products[indexPath.row] 
     let imageName = "bag.png" 
     let image = UIImage(named: imageName) 
     cell.imageView!.image = image 
     cell.detailTextLabel?.text = "indexpath.row\(indexPath.row)" 
     return cell 
    default: 
     let cell = self.tableView.dequeueReusableCellWithIdentifier("ProductCell", forIndexPath: indexPath) as! 
     UITableViewCell 
     cell.textLabel?.text = "indexpath.row\(indexPath.row)" 
     return cell 


    } 

} 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    switch flag 
    { 
    case 0: 
     return 1 
    case 1: 
     return 1 
    case 2: 
     return farmers.count 
    default: 
     return 1 
    } 
} 

//segue 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

} 

//personal functions 
func calculateHeightForString(inString:String) -> CGFloat 
{ 
    var messageString = inString 
    var attributes = [UIFont(): UIFont.systemFontOfSize(15.0)] 
    var attrString:NSAttributedString? = NSAttributedString(string: messageString, attributes: attributes) 
    var rect:CGRect = attrString!.boundingRectWithSize(CGSizeMake(300.0,CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, context:nil) 
    var requredSize:CGRect = rect 
    return requredSize.height //to include button's in your tableview 
} 

статья класс

import UIKit 

class ArticleCell: UITableViewCell, UITextViewDelegate { 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

@IBOutlet weak var textView: UITextView! 
override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 
func SetText(inString: String) 
{ 
    textView.text = inString 
} 
+0

- это ваш текстовый выход, подключенный в вашем xib? – Lucho

ответ

0

Попробуйте установить делегат перед тем, как изменить текст, в методе cellForRowAtIndexPath. Должно быть что-то вроде

cell.textView.delegate = self

Смежные вопросы