我正在使用Swift在IOS中创建一个简单的文章应用程序。我在更新textview中的文本时遇到问题。但我在自定义TableViewCell类中有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
}
文章课
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 :(得分:0)
在cellForRowAtIndexPath方法中尝试在更改文本之前设置委托。应该像
cell.textView.delegate = self