ios - 使用自定义 UITableViewCells 并使用 Swift 数组复制它们
问题描述
我正在开发一个 iOS 应用程序,我有一个基于数组UITableView
生成s 的应用程序。UITableViewCell
换句话说,我有一个自定义单元格,里面有UIViews
。如何添加一个包含 Repo 标签文本和 URL 标签文本的二维数组,并生成大量带有array.count
.
这是我的代码:
class SourcesViewController: UITableViewController {
var array:[[String]] = [["Thunderbolt iOS Utilities", "https://repo.thunderbolt.semiak.dev"], ["Semiak's Repo", "repo.semiak.dev"]]
override func viewDidLoad()
{
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
//How can I setup all the arguments my custom UITableViewCell needs?
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
}
对不起,如果解释真的很糟糕,我以前从未做过这件事,而且我是 iOS 开发的新手,所以我不知道如何准确地解释我需要什么。
解决方案
这是一个起点(我不建议将此代码用于生产。有更好的方法(例如,使用结构数组,将代码放入自定义单元格等))
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let dataArray = array[indexPath.row] // get the data for the cell
// not a great idea but....
cell.repoLabel.text = dataArray.first ?? ""
cell.urlLabel.text = dataArray.last ?? ""
return cell
}
推荐阅读
- c# - 寻找客户端 Json/WebAPI 包来发送 HttpGet/HttpPost 消息
- node.js - 如何在 Angular 中修复“错误:路径”/__path__/__name@dasherize@if-flat“不存在”
- c++ - 在 JSON 字符串中序列化 utf-8 字符的标准方法
- android - 如何在 Dokka 中设置输出 html 全局语言属性?
- git - 在给定的提交 ID 之前列出提交
- java - 如何在单独的进程中在 gradle 中运行并行集成测试
- ios - Chirp Audio QR 从后台恢复后 ipad 无法接收音频信号
- javascript - 使用 typescript 访问 DOM 类或 id 的更好方法
- bash - 如何防止 `source .bashrc` 在具有正则表达式的别名命令中删除转义?
- javascript - 如何在 React-MapBox-GL 中添加 GeoJson 层