【问题标题】:TableviewCells with different contents不同内容的 TableviewCells
【发布时间】:2016-05-10 13:40:18
【问题描述】:

我正在尝试制作一个应用程序,用户可以在其中添加一个单元格,并且他可以在其中向单元格添加不同的内容,例如姓名、生日和爱好。 到现在为止还挺好。 但是我怎样才能向他展示每个单元格的各个内容? 我是否必须添加具有不同标签的 ViewController,在其中加载为特定单元格保存的文本,例如使用 NSuserdefaults/Coredata? 还是我完全错了?

我现在拥有的:在我的 viewController 中我可以添加一个项目

@IBAction func doneButtonPressed(sender: AnyObject) {
    name = txtFieldName.text!
    vc.items.append(name)
    vc.tableView.reloadData()
    self.dismissViewControllerAnimated(false, completion:nil)
}

在我的 tableviewController 中:

  var items = [String]();

override func viewDidLoad() {
    super.viewDidLoad()
    print(items)
}

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

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("itemCell", forIndexPath: indexPath)

    cell.textLabel!.text = items[indexPath.row]
    return cell
}

【问题讨论】:

  • 请提供一些示例代码以使这个问题更清楚。
  • 现在如何在创建每个单元格时保存每个单元格的数据以及如何为每个单元格加载不同的内容。

标签: swift uitableview core-data cell nsuserdefaults


【解决方案1】:

你需要实现这个东西

NavigationController ->(segue) TableView 或 TVController ->(show item segue) TableView 或 TVController

使用这个例子,我们应该为每个表格视图和单元格创建文件 第一对(newsvc.swift newscell.swift) 第二对(newsitemvc.swift newsitemcell.swift)

所以我们理解它是一个 MVC

我们需要实现一些静态数据 创建模型文件(添加结构并初始化)

struct News {
    var time: String?
    var title: String?
    var text: String?

    init(time: String?, title: String?,text: String?) {
        self.time = time
        self.title = title
        self.text = text
    }
}

在第二个文件中,我们为结构创建数据

let newsData = [ News(time: "10:00", title: "Test", text: "Some text"),
                 News(time: "11:00", title: "Test1", text: "Some text1")]

NewsCell.swift

class NewsCell: UITableViewCell {

  @IBOutlet weak var timeLabel: UILabel!

  @IBOutlet weak var titleLabel: UILabel!

  @IBOutlet weak var textLabel: UILabel!

  var news: News! {
    didSet {
      timeLabel = news.time

      titleLabel = news.title

      textLabel.text = news.text
    }
  }
}

在 NewsViewController 中我们需要添加“News”数组并使其从“newsData”中获取数据

var newsItems: [News] = newsData

更新函数

  func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
  }

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return newsItems.count
  }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! NewsCell
    let news = newsItems[indexPath.row] as News
    cell.news = news
    return cell
  }

在它之后我们应该添加segue

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow!
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    if segue.identifier == "showItemSegue" {
      if let destViewController = segue.destinationViewController as? NewsItemViewController {
      let seguedArray = newsItems[indexPath.row] as News
      destViewController.newSeguedArray = seguedArray
      }
    }
    }

【讨论】:

  • 好的,如何保存创建单元格的数据?我可以使用 NSuserdefaults 还是必须使用 CoreData
  • @Korken55 现在你明白了吗?
  • 好吧,我会试试这个。我现在不在家。谢谢那个
  • 我的第二个文件可以是 UIViewController,用户可以在其中提供时间和文本等变量的数据吗?因为我的问题是我想为用户创建的每个单元格保存数据。
【解决方案2】:

在将数据设置到所述单元格之前,您可以将数据附加到数组中。您可以使用数组中的索引和index.row 变量,根据他们点击的内容提取这些数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多