【发布时间】:2017-02-10 05:26:32
【问题描述】:
大家好,我在右侧navigationBarItem 有一个按钮 (+),我正在尝试创建一个功能,当您单击此按钮时,您会在 UITableViewCell 中创建一个cell,您可以给它一个title 和一个图像。
【问题讨论】:
-
显示用于表格视图的数据源代码。
标签: swift xcode uitableview swift3 tableviewcell
大家好,我在右侧navigationBarItem 有一个按钮 (+),我正在尝试创建一个功能,当您单击此按钮时,您会在 UITableViewCell 中创建一个cell,您可以给它一个title 和一个图像。
【问题讨论】:
标签: swift xcode uitableview swift3 tableviewcell
我将首先创建一个结构来表示您的单元格的数据。
struct Object {
var image: UIImage!
var title: String!
}
然后你可以有一个Object 的数组作为你的表格视图的数据源。
class ViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
var object: Object?
var objects: [Object] = []
var picker: UIImagePickerController!
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
picker = UIImagePickerController()
picker?.allowsEditing = false
picker?.delegate = self
picker?.sourceType = .photoLibrary
}
// MARK: UITableViewDataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
let object = objects[indexPath.row]
cell.imageView?.image = object.image ?? UIImage()
cell.textLabel?.text = object.title ?? ""
return cell
}
// MARK: UIImagePickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
switch info[UIImagePickerControllerOriginalImage] as? UIImage {
case let .some(image):
object?.image = image
default:
break
}
picker.dismiss(animated: true) {
self.showCellTitleAlert()
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
object = nil
dismiss(animated: true) {
self.tableView.reloadData()
}
}
// MARK: Alerts
private func showCellTitleAlert() {
let alert = UIAlertController(title: "Cell Title", message: nil, preferredStyle: .alert)
alert.addTextField { $0.placeholder = "Title" }
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) {
self.object = nil
})
alert.addAction(UIAlertAction(title: "Save", style: .default) { _ in
self.object?.title = alert.textFields?.first.flatMap { $0.text }
self.object.flatMap { self.objects.append($0) }
self.tableView.reloadData()
})
present(alert, animated: true, completion: nil)
}
// MARK: Actions
@IBAction func didSelectCreateButton() {
object = Object()
present(picker, animated: true, completion: nil)
}
}
您将需要一个图像选择器供用户选择图像,并且您可以使用警报视图来获取用户输入的标题。通过符合UIImagePickerControllerDelegate,您可以检测图像何时被拾取或用户是否取消。
按下创建按钮时,我们会创建一个空对象并显示选取器。在我们用所选图像填充对象后,我们会显示文本字段警报视图。如果按下保存按钮,则将对象添加到数据源并重新加载表视图。
【讨论】:
您可以更新数据源中的值并使用tableView.reloadData()。这将重新加载表格并向表格添加一个新单元格。因此,您也可以给它一个标题和图像。
【讨论】:
只更新数据源的计数
创建一个变量
var rowCount:Int = 0
添加改变变量值的函数
@IBAction func addCell(sender:AnyObject) {
rowCount = rowCount + 1
tableView.reloadData()
}
在表格视图的数据源委托方法中,使用这个变量
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
return rowCount
}
在委托方法中设置标题和图片
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//add logic here
return cell
}
【讨论】: