优点和缺点列表 Swift
Pros and Cons list Swift
我正在尝试使用 table 视图在 swift 中列出优缺点。我得到了第一个列表,但是当我添加第二个时,它只是复制了第一个。
import UIKit
class prosConsViewController: UIViewController
{
@IBOutlet var tableViewOutlet: UITableView!
var colleges : [NetCollege] = []
@IBOutlet var consTableView: UITableView!
var collegesTwo : [NetCollegeTwo] = []
override func viewDidLoad()
{
super.viewDidLoad()
editButtonItem().tag = 0
}
@IBAction func onTappedPlusButton(sender: UIBarButtonItem)
{
var alert = UIAlertController(title: "Add Pro", message: nil, preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler
{ (textField) -> Void in
textField.placeholder = "Add Pro Here"
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alert.addAction(cancelAction)
var addAction = UIAlertAction(title: "Add", style: .Default) { (action) -> Void in
var addCollegesTextField = alert.textFields?[0] as! UITextField
var netCollege = NetCollege(name: addCollegesTextField.text)
self.colleges.append(netCollege)
self.tableViewOutlet.reloadData()
}
alert.addAction(addAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!
{
let cell = tableViewOutlet.dequeueReusableCellWithIdentifier("cellID") as! tableViewCell
//the line under maybe?
var college = colleges[indexPath.row]
cell.textLabel?.text = college.name
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return colleges.count
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == UITableViewCellEditingStyle.Delete
{
colleges.removeAtIndex(indexPath.row)
tableView.reloadData()
}
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
{
var collegeList = colleges[sourceIndexPath.row]
colleges.removeAtIndex(sourceIndexPath.row)
colleges.insert(collegeList, atIndex: destinationIndexPath.row)
}
缺点列表从这里开始
@IBAction func plusButtonTwo(sender: UIBarButtonItem)
{
var alertTwo = UIAlertController(title: "Add Con", message: nil, preferredStyle: .Alert)
alertTwo.addTextFieldWithConfigurationHandler
{ (textField) -> Void in
textField.placeholder = "Add Con Here"
}
var cancelActionTwo = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertTwo.addAction(cancelActionTwo)
var addActionTwo = UIAlertAction(title: "Add", style: .Default) { (action) -> Void in
var addCollegesTextFieldTwo = alertTwo.textFields?[0] as! UITextField
var netCollegeTwo = NetCollegeTwo(nameTwo: addCollegesTextFieldTwo.text)
self.collegesTwo.append(netCollegeTwo)
self.consTableView.reloadData()
}
alertTwo.addAction(addActionTwo)
self.presentViewController(alertTwo, animated: true, completion: nil)
}
func consTableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!
{
let cellTwo = consTableView.dequeueReusableCellWithIdentifier("IDCell") as! tableViewCell
var collegeTwo = collegesTwo[indexPath.row]
cellTwo.textLabel?.text = collegeTwo.conName
return cellTwo
}
func consTableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return collegesTwo.count
}
func consTableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == UITableViewCellEditingStyle.Delete
{
collegesTwo.removeAtIndex(indexPath.row)
consTableView.reloadData()
}
}
func consTableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func consTableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
{
var collegeListTwo = collegesTwo[sourceIndexPath.row]
collegesTwo.removeAtIndex(sourceIndexPath.row)
collegesTwo.insert(collegeListTwo, atIndex: destinationIndexPath.row)
}
}
您不能仅将 UITableViewDelegate
/UITableViewDataSource
方法的签名从 tableView:cellForRowAtIndexPath:
更改为 consTableView:cellForRowAtIndexPath:
并期望它起作用。两个 table 都会调用 tableView:cellForRowAtIndexPath:
并显示相同的结果。
如果您希望同一个对象成为您的 table 的委托和数据源,您需要为您的 table 和在您的委托和数据源方法实现中放置一个 if
语句,以根据调用该方法的 table 视图提供不同的行为。
例如:
class prosConsViewController: UIViewController {
@IBOutlet var prosTableViewOutlet: UITableView!
@IBOutlet var consTableViewOutlet: UITableView!
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! {
if tableView == prosTableViewOutlet {
// Pros logic
}
else {
// Cons logic
}
}
}
我正在尝试使用 table 视图在 swift 中列出优缺点。我得到了第一个列表,但是当我添加第二个时,它只是复制了第一个。
import UIKit
class prosConsViewController: UIViewController
{
@IBOutlet var tableViewOutlet: UITableView!
var colleges : [NetCollege] = []
@IBOutlet var consTableView: UITableView!
var collegesTwo : [NetCollegeTwo] = []
override func viewDidLoad()
{
super.viewDidLoad()
editButtonItem().tag = 0
}
@IBAction func onTappedPlusButton(sender: UIBarButtonItem)
{
var alert = UIAlertController(title: "Add Pro", message: nil, preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler
{ (textField) -> Void in
textField.placeholder = "Add Pro Here"
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alert.addAction(cancelAction)
var addAction = UIAlertAction(title: "Add", style: .Default) { (action) -> Void in
var addCollegesTextField = alert.textFields?[0] as! UITextField
var netCollege = NetCollege(name: addCollegesTextField.text)
self.colleges.append(netCollege)
self.tableViewOutlet.reloadData()
}
alert.addAction(addAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!
{
let cell = tableViewOutlet.dequeueReusableCellWithIdentifier("cellID") as! tableViewCell
//the line under maybe?
var college = colleges[indexPath.row]
cell.textLabel?.text = college.name
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return colleges.count
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == UITableViewCellEditingStyle.Delete
{
colleges.removeAtIndex(indexPath.row)
tableView.reloadData()
}
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
{
var collegeList = colleges[sourceIndexPath.row]
colleges.removeAtIndex(sourceIndexPath.row)
colleges.insert(collegeList, atIndex: destinationIndexPath.row)
}
缺点列表从这里开始
@IBAction func plusButtonTwo(sender: UIBarButtonItem)
{
var alertTwo = UIAlertController(title: "Add Con", message: nil, preferredStyle: .Alert)
alertTwo.addTextFieldWithConfigurationHandler
{ (textField) -> Void in
textField.placeholder = "Add Con Here"
}
var cancelActionTwo = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertTwo.addAction(cancelActionTwo)
var addActionTwo = UIAlertAction(title: "Add", style: .Default) { (action) -> Void in
var addCollegesTextFieldTwo = alertTwo.textFields?[0] as! UITextField
var netCollegeTwo = NetCollegeTwo(nameTwo: addCollegesTextFieldTwo.text)
self.collegesTwo.append(netCollegeTwo)
self.consTableView.reloadData()
}
alertTwo.addAction(addActionTwo)
self.presentViewController(alertTwo, animated: true, completion: nil)
}
func consTableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!
{
let cellTwo = consTableView.dequeueReusableCellWithIdentifier("IDCell") as! tableViewCell
var collegeTwo = collegesTwo[indexPath.row]
cellTwo.textLabel?.text = collegeTwo.conName
return cellTwo
}
func consTableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return collegesTwo.count
}
func consTableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == UITableViewCellEditingStyle.Delete
{
collegesTwo.removeAtIndex(indexPath.row)
consTableView.reloadData()
}
}
func consTableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func consTableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
{
var collegeListTwo = collegesTwo[sourceIndexPath.row]
collegesTwo.removeAtIndex(sourceIndexPath.row)
collegesTwo.insert(collegeListTwo, atIndex: destinationIndexPath.row)
}
}
您不能仅将 UITableViewDelegate
/UITableViewDataSource
方法的签名从 tableView:cellForRowAtIndexPath:
更改为 consTableView:cellForRowAtIndexPath:
并期望它起作用。两个 table 都会调用 tableView:cellForRowAtIndexPath:
并显示相同的结果。
如果您希望同一个对象成为您的 table 的委托和数据源,您需要为您的 table 和在您的委托和数据源方法实现中放置一个 if
语句,以根据调用该方法的 table 视图提供不同的行为。
例如:
class prosConsViewController: UIViewController {
@IBOutlet var prosTableViewOutlet: UITableView!
@IBOutlet var consTableViewOutlet: UITableView!
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! {
if tableView == prosTableViewOutlet {
// Pros logic
}
else {
// Cons logic
}
}
}