自定义单元格 TextVIew 文本未更改

Custom Cell TextVIew text not changing

我正在使用 Swift 在 IOS 中创建一个简单的文章应用程序。我在更新文本视图中的文本时遇到问题。但我在自定义 TableViewCell class 中有 textView,无法弄清楚如何更改文本。我还尝试制作 setter 函数。我没有错误日志,我在创建单元格和更改文本后打印单元格的内容。当我创建它时,它有占位符文本,在我更改它之后,它在 cellforRow 的单元格中发生了更改,但实际显示的是来自 xib 的文本。

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
}

文章class

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
}

尝试在更改文本之前在 cellForRowAtIndexPath 方法中设置委托。应该是

cell.textView.delegate = 自己