swift - 具有不同内容的表格 View 单元格

标签 swift uitableview core-data cell nsuserdefaults

我正在尝试制作一个应用程序,用户可以在其中添加一个单元格,并且他可以在其中向单元格添加不同的内容,例如姓名、生日和爱好。 到目前为止,一切都很好。 但是我怎样才能向他展示每个单元格的单独内容呢? 我是否必须添加具有不同标签的 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
}

最佳答案

你需要实现这个东西

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

使用这个例子,我们应该为每个表格 View 和单元格创建文件 第一对(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
      }
    }
    }

enter image description here

enter image description here

关于swift - 具有不同内容的表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37140312/

相关文章:

ios - 如何在 Swift 中使用 NSCoding 保存 Int64 变量数组

ios - 如何访问不属于 iOS 主包的文件

ios - 如何在 UINavigationBar 下方添加 UISegmentControl?

ios - Swift 3 核心数据 NSExpression 始终返回 0

xcode - 'AppDelegate' 没有名为 'managedObjectContext' 的成员

swift - 尝试编写一个通用函数来将 JSON 解析为可编码的结构

ios - UIActivityViewController 主题不适用于 Outlook

swift - 取消 Swift 中的定时事件?

ios - 后退后 UISearchBar 错误

ios - UITableView可以上下拖动,不允许Swipe Gesture?