Saving input from alert message-swift - ios

I have an issue that prevents me from display the input from the alert message into the table view. There are no errors but I don't know what's the real problem that makes the table empty.
import UIKit
import Parse
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
#IBOutlet weak var childTbl: UITableView!
var names=[String] ()
override func viewDidLoad() {
super.viewDidLoad()
self.childTbl.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
#IBAction func Addchild(sender: AnyObject) {
var alert=UIAlertController(title: "",
message: "إضافة طفل جديد",
preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler({(textField) -> Void in
textField.text=""
})
alert.addAction((UIAlertAction(title: "حفظ", style: .Default, handler: { (ACTION) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
if let textField = alert.textFields!.first as?UITextField{
self.names.append(textField.text)
println(self.names)
}
})))
alert.addAction((UIAlertAction(title: "إلغاء", style: .Default, handler: { (ACTION) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})))
//to display the alert
presentViewController(alert,
animated: true,
completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//UITabledatasource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.names.count;
}
//function to creat a cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell:UITableViewCell = self.childTbl.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = self.names[indexPath.row]
return cell
}
}

Related

Swift: How to create UIActionSheet with click on button UITableVieCell

I have a tableview with buttons, and I would like to create a UIActionSheet here when I click on the 3 dots button. It is a custome tableview cell.
My UITableViewCell:
import UIKit
class UserTableViewCell: UITableViewCell {
#IBOutlet weak var nameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func view(with user: User){
nameLabel.text = user.getName();
}
#IBAction func btnMenu(_ sender: UIButton) {
//here I want to execute the UIActionSheet
}
#IBAction func btnDial(_ sender: UIButton) {
}
}
and in my view controller:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as? UserTableViewCell;
cell?.view(with: users[indexPath.row]);
return cell!;
}
Try this and do some changes in UserTableViewCell
class UserTableViewCell: UITableViewCell {
weak var myVC : UIViewController?
#IBAction func btnMenu(_ sender: UIButton) {
//here I want to execute the UIActionSheet
let actionsheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
actionsheet.addAction(UIAlertAction(title: "Take a Photo", style: UIAlertActionStyle.default, handler: { (action) -> Void in
}))
actionsheet.addAction(UIAlertAction(title: "Choose Exisiting Photo", style: UIAlertActionStyle.default, handler: { (action) -> Void in
}))
actionsheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (action) -> Void in
}))
myVC?.present(actionsheet, animated: true, completion: nil)
}
}
And modify this method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as? UserTableViewCell;
cell?.view(with: users[indexPath.row]);
cell?.myVC = self
return cell!;
}
make outlets of button in cell class
then in tableView where you are using this cell write the code below in cellForRowAtIndexPath
cell.yourButton.addTarget(self, action: #selector(yourButtonTapped), for: .touchDown)
and now in your yourButtonTapped method write actionSheet code following the link :
UIActionSheet iOS Swift
hope its help
Closure Approach
1 - Declare your actionBlock in your UserTableViewCell
var actionClosure : (()->Void)? = nil
2 - Execute your action block in your Cell Action
#IBAction func btnMenu(_ sender: UIButton) {
//here I want to execute the UIActionSheet
self.actionClosure?()
}
3 - Setup your cell block action adjusting your cellForRowAtIndexPath method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserTableViewCell", for: indexPath) as! UserTableViewCell
cell.actionClosure = { [weak self] in
//SHow your ActionSheet Here
}
return cell
}
Full Code
CellForRow implementation
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserTableViewCell", for: indexPath) as! UserTableViewCell
cell.actionClosure = { [weak self] in
//SHow your ActionSheet Here
}
return cell
}
TableView Cell
import UIKit
class UserTableViewCell: UITableViewCell {
#IBOutlet weak var nameLabel: UILabel!
var actionClosure : (()->Void)? = nil
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func view(with user: User){
nameLabel.text = user.getName();
}
#IBAction func btnMenu(_ sender: UIButton) {
//here I want to execute the UIActionSheet
self.actionClosure?()
}
#IBAction func btnDial(_ sender: UIButton) {
}
}
just add onMenubtnClick method in your ViewControler instead of cell.
add this in your cellForRowAt method
cell.youtBtn.addTarget(self, action: #selector(self.btnMenu(_:)), for: .touchUpInside)
add this code in your ViewController
#IBAction func btnMenu(_ sender: UIButton) {
//here I want to execute the UIActionSheet
}

UiTableView number of generated cells

I have two viewControllers. The first with a stepper:
var steppers : UIStepper?
#IBOutlet weak var hourLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBAction func stepper(_ sender: UIStepper) {
hourLabel.text = String(sender.value)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let np = segue.destination as? CourseClass2 {
np.numberPlaces = Int(stepper.value)
}
}
and the second with a tableView:
private let resueIdentifier = "MyTableViewCell"
extension UIViewController {
func present(viewController : UIViewController, completion : (() -> ())? = nil ){
if let presented = self.presentedViewController {
presented.dismiss(animated: true, completion: {
self.present(viewController, animated: true, completion: completion)
})
} else {
self.present(viewController, animated: true, completion: completion)
}
}
}
class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var locationManager:CLLocationManager?
let minimumSpacing : CGFloat = 15 //CGFloat(MAXFLOAT)
let cellWidth: CGFloat = 250
let radius = 5000 // 5km
var category : QCategoryy?
var currentLocation : CLLocationCoordinate2D?
var places: [QPlace] = []
var isLoading = false
var response : QNearbyPlacesResponse?
var rows = 0
var numberPlaces = 0
override func viewDidLoad() {
super.viewDidLoad()
self.title = category?.name
tableView.dataSource = self
tableView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
determineMyCurrentLocation()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
rows = 0
/* insertRowsMode3()
tableView.reloadData() */
category?.markView()
}
#IBAction func refreshTapped(_ sender: Any) {
rows = 0
/* insertRowsMode3() */
tableView.reloadData()
}
func canLoadMore() -> Bool {
if isLoading {
return false
}
if let response = self.response {
if (!response.canLoadMore()) {
return false
}
}
return true
}
func loadPlaces(_ force:Bool) {
if !force {
if !canLoadMore() {
return
}
}
print("load more")
isLoading = true
NearbyPlaces.getNearbyPlaces(by: category?.name ?? "food", coordinates: currentLocation!, radius: radius, token: self.response?.nextPageToken, completion: didReceiveResponse)
}
func didReceiveResponse(response:QNearbyPlacesResponse?, error : Error?) -> Void {
if let error = error {
let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
let actionDismiss = UIAlertAction(title: "Dismiss", style: .cancel, handler: nil)
let actionRetry = UIAlertAction(title: "Retry", style: .default, handler: { (action) in
DispatchQueue.main.async {
self.loadPlaces(true)
}
})
alertController.addAction(actionRetry)
alertController.addAction(actionDismiss)
DispatchQueue.main.async {
self.present(viewController: alertController)
}
}
if let response = response {
self.response = response
if response.status == "OK" {
if let placesDownloaded = response.places {
places.append(contentsOf: placesDownloaded)
}
self.tableView?.reloadData()
} else {
let alert = UIAlertController.init(title: "Error", message: response.status, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction.init(title: "Retry", style: .default, handler: { (action) in
DispatchQueue.main.async {
self.loadPlaces(true)
}
}))
self.present(viewController: alert)
}
isLoading = false
}
else {
print("response is nil")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
print("numberOfsection Call")
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numberOfRows Call")
return places.count /* rows */
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: resueIdentifier, for: indexPath) as! MyTableViewCell
let place = places[indexPath.row]
cell.update(place: place)
if indexPath.row == places.count - 1 {
loadPlaces(false)
}
print("CellForRow Call")
return (cell)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
UIView.animate(withDuration: 0.2, animations: {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as! MyTableViewCell
})
performSegue(withIdentifier: "goToLast" , sender: indexPath.row)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
places.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
func didReceiveUserLocation(_ userLocation:CLLocation) {
currentLocation = userLocation.coordinate
loadPlaces(true)
}
I added only the code useful for the question. As you can see I'm trying to pass from the first VC to the second this value:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let np = segue.destination as? CourseClass2 {
np.numberPlaces = Int(stepper.value)
}
}
Because I would like to add a limit to the number of cells that spawn when loading the tableview in the second viewController (and I want this limit to be chosen by the user with the stepper in the previous controller), how do I have to modify the tableView to make sure that the number of cells generated is equal to the value of var numberPlaces?
I would suggest that you limit the number of cells inside UITableView delegate method number of cells
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numberOfRows Call")
if places.count < self.numberPlaces {
return places.count /* rows */
}
return self.numberPlaces
}
In this way, you will get right number of cells, but if there would be too many places only self.numberPlaces will be displayed.
Maybe, you would need to figure out some sorting, so that correct places would be displayed. (By proximity,...)

How to locate the line? unexpectedly found nil

When I try to run my app, I get the error stating:
fatal error: unexpectedly found nil while unwrapping an Optional
value.
Can anybody tell me if its a way to locate the line where the problem is?
Unfortunately, I don't get a red line where the simulator crashes.
I pasted in all of the code, but the problem must have to do with the alert function because it worked fine until I tried to implement that.
import UIKit
var list = ["Visa code: 1234", "Mastercard code: 4321"]
class notesVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var messageLabel: UILabel!
var userMessage = "Sample text"
var theUserText: UITextField?
#IBOutlet weak var tabelView: UITableView!
#IBAction func addItemButton(_ sender: Any) {
let alertController = UIAlertController(title:"title",
message: "message",
preferredStyle: .alert)
alertController.addTextField(configurationHandler: theUserTextFunc)
let okAction = UIAlertAction(title: "OK",
style: .default,
handler: self.okHandler)
let cancleAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancleAction)
self.present(alertController, animated: true)
}
func theUserTextFunc(textField: UITextField){
theUserText = textField
}
func okHandler(alert: UIAlertAction!){
list.append((theUserText?.text)!)
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return (list.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return(cell)
}
// Swipe to delete an item
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
list.remove(at: indexPath.row)
tabelView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
messageLabel.text = userMessage
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func customInit(userMessage: String) {
self.userMessage = userMessage
}
}
I tried your code. It works flawless except that you missed to reload on table on OkHandler.
So I suspect issue would be with ur IBOutlet or IBAction connections. Check around that...
import UIKit
var list = ["Visa code: 1234", "Mastercard code: 4321"]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
#IBOutlet weak var messageLabel: UILabel!
var userMessage = "Sample text"
var theUserText: UITextField?
#IBOutlet weak var tabelView: UITableView!
#IBAction func addItemButton(_ sender: Any) {
let alertController = UIAlertController(title:"title",
message: "message",
preferredStyle: .alert)
alertController.addTextField(configurationHandler: theUserTextFunc)
let okAction = UIAlertAction(title: "OK",
style: .default,
handler: self.okHandler)
let cancleAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancleAction)
self.present(alertController, animated: true)
}
func theUserTextFunc(textField: UITextField){
theUserText = textField
}
func okHandler(alert: UIAlertAction!){
list.append((theUserText?.text)!)
self.tabelView.reloadData()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return (list.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return(cell)
}
// Swipe to delete an item
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
list.remove(at: indexPath.row)
tabelView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
messageLabel.text = userMessage
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func customInit(userMessage: String) {
self.userMessage = userMessage
}
}
Try changing your add code
alertController.addTextField(configurationHandler: theUserTextFunc)
to -
alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
textField.placeholder = "Search"
// Set other textfield values that you want
})

how to get data to save to specific category in tableview instead on all categories?

I have a groceryList app
when you add an item to the category list it adds to the entire list of categories when is should not!
https://github.com/mrbryankmiller/Grocery-TableView-.git
class GroceryItemsTableViewController: UITableViewController {
//var groceryItem = ["Item1", "Item2", "Item3"]
//var groceryList = ["Breakfast","Lunch", "Dinner"]
#IBOutlet var groceryItemTableView: UITableView!
#IBAction func addGroceryItemButtonPressed(sender: UIBarButtonItem) {
///new way///
let alertController: UIAlertController = UIAlertController(title: "Add Grocery Item", message: "", preferredStyle: .Alert)
//Cancel Button
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//cancel code
}
alertController.addAction(cancelAction)
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
let textField = alertController.textFields![0]
groceryItem.items.append(textField.text!)
self.tableView.reloadData()
}
alertController.addAction(saveAction)
//Add text field
// alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
// textField.textColor = UIColor.blackColor()
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
textField.placeholder = "Enter an Item"
//alertController.textFields
}
//Present the AlertController
self.presentViewController(alertController, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
//self.navigationItem.leftBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return groceryItem.items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("groceryItem1", forIndexPath: indexPath)
cell.textLabel!.text = groceryItem.items [indexPath.row]
return cell
}
}
If you see carefully the declaration of your class groceryItem you have a static array of elements for every item in your grocery list so every time you add a new element it's shared among all the grocery items.
Instead you should have for each grocery item a list associated with each of its items.
You could define a new struct to save for each grocery item its list of item associated like in the following way:
struct GroceryItem {
var name: String
var items: [String]
}
The we are going to change a little the code in your GroceryListTableViewController to refactor the code according your new model, so it should be like the following:
GroceryListTableViewController:
class GroceryListTableViewController: UITableViewController, GroceryItemsTableViewControllerProtocol {
var groceryList = [GroceryItem]()
#IBAction func addButton(sender: UIBarButtonItem) {
let alertController: UIAlertController = UIAlertController(title: "Add Grocery Category", message: "", preferredStyle: .Alert)
//Cancel Button
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//cancel code
}
alertController.addAction(cancelAction)
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
let textField = alertController.textFields![0]
self.groceryList.append(GroceryItem(name: textField.text!, items: [String]()))
self.tableView.reloadData()
}
alertController.addAction(saveAction)
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
textField.placeholder = "Enter an Item"
//alertController.textFields
}
//Present the AlertController
self.presentViewController(alertController, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
//edit button
self.navigationItem.leftBarButtonItem = self.editButtonItem()
groceryList.append(GroceryItem(name: "Breakfast", items: ["Item1", "Item2", "Item3"]))
groceryList.append(GroceryItem(name: "Lunch", items: ["Item1", "Item2", "Item3"]))
groceryList.append(GroceryItem(name: "Dinner", items: ["Item1", "Item2", "Item3"]))
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return groceryList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("prototype1", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = groceryList [indexPath.row].name
return cell
}
// pass a tableview cell value to navigationBar title in swift//
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! GroceryItemsTableViewController
let cell = sender as! UITableViewCell
let idx = self.tableView.indexPathForSelectedRow?.row
destinationVC.delegate = self
destinationVC.itemList = groceryList[idx!].items
destinationVC.navigationItem.title = cell.textLabel?.text
}
func didAddGroceryItem(itemName: String) {
let idx = self.tableView.indexPathForSelectedRow?.row
groceryList[idx!].items.append(itemName)
}
func didRemoveGroceryItem(index: Int) {
let idx = self.tableView.indexPathForSelectedRow?.row
groceryList[idx!].items.removeAtIndex(index)
}
}
In the above I have refactored all the code regarding the new model, I put only the places where the code change the rest keep the same.
The thing you need to pass the item associated with the cell selected to the another UIViewController and you can do it very easily in your prepareForSegue. For that we need to get the index for the selected cell and pass the elements to the another UIViewController where we have a new array of [String] created as data source to show the items.
The another important point in the code is that the GroceryListTableViewController now implements a new protocol called GroceryItemsTableViewControllerProtocol. This protocol it's the way to notify to GroceryListTableViewController from the GroceryItemsTableViewController every time a new item is added to the list it's called the delegate pattern.
GroceryItemsTableViewController:
protocol GroceryItemsTableViewControllerProtocol: class {
func didAddGroceryItem(itemName: String)
func didRemoveGroceryItem(index: Int)
}
class GroceryItemsTableViewController: UITableViewController {
weak var delegate: GroceryItemsTableViewControllerProtocol?
var itemList: [String]!
#IBAction func addGroceryItemButtonPressed(sender: UIBarButtonItem) {
///new way///
let alertController: UIAlertController = UIAlertController(title: "Add Grocery Item", message: "", preferredStyle: .Alert)
//Cancel Button
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//cancel code
}
alertController.addAction(cancelAction)
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { [weak self] action -> Void in
guard let s = self else { return }
let textField = alertController.textFields![0]
s.itemList.append(textField.text!)
s.delegate?.didAddGroceryItem(textField.text!)
s.tableView.reloadData()
}
alertController.addAction(saveAction)
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
textField.placeholder = "Enter an Item"
//alertController.textFields
}
//Present the AlertController
self.presentViewController(alertController, animated: true, completion: nil)
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("groceryItem1", forIndexPath: indexPath)
cell.textLabel!.text = itemList[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
itemList.removeAtIndex(indexPath.row)
delegate?.didRemoveGroceryItem(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
}
EDIT:
To handle properly the deletion you should create a new delegate method no notify the GroceryListTableViewController that a item has been deleted and then delete it properly and you can see in the updated code above.
I hope this help you.

two table views in one viewcontroller swift

I'm trying to make a pros and cons list in swift, but whenever I delete a con it deletes a pro. I think that it is a problem with index path being linked to both the pros and cons view controller but I don't know how or where I can separate them
class prosConsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
#IBOutlet var prosTableViewOutlet: UITableView!
#IBOutlet var consTableViewOutlet: UITableView!
#IBOutlet var tableViewOutlet: UITableView!
var colleges : [NetCollege] = []
#IBOutlet var consTableView: UITableView!
var collegesTwo : [NetCollegeTwo] = []
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if tableView == tableViewOutlet
{
return colleges.count
}
else
{
return collegesTwo.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
if tableView == tableViewOutlet
{
let cell = tableViewOutlet.dequeueReusableCellWithIdentifier("cellID") as! tableViewCell
//the line under maybe?
let college = colleges[indexPath.row]
cell.textLabel?.text = college.name
return cell
}
else
{
let cellTwo = consTableView.dequeueReusableCellWithIdentifier("IDCell") as! tableViewCell
let collegeTwo = collegesTwo[indexPath.row]
cellTwo.textLabel?.text = collegeTwo.conName
return cellTwo
}
}
override func viewDidLoad()
{
super.viewDidLoad()
editButtonItem().tag = 0
func shouldAutorotate() -> Bool {
return false
}
func supportedInterfaceOrientations() -> Int {
return UIInterfaceOrientation.LandscapeRight.rawValue
}
}
#IBAction func plusButtonTwo(sender: UIBarButtonItem)
{
let alertTwo = UIAlertController(title: "Add Con", message: nil, preferredStyle: .Alert)
alertTwo.addTextFieldWithConfigurationHandler
{ (textField) -> Void in
textField.placeholder = "Add Con Here"
}
let cancelActionTwo = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertTwo.addAction(cancelActionTwo)
let addActionTwo = UIAlertAction(title: "Add", style: .Default) { (action) -> Void in
let addCollegesTextFieldTwo = (alertTwo.textFields?[0])! as UITextField
let netCollegeTwo = NetCollegeTwo(nameTwo: addCollegesTextFieldTwo.text!)
self.collegesTwo.append(netCollegeTwo)
self.consTableView.reloadData()
}
alertTwo.addAction(addActionTwo)
self.presentViewController(alertTwo, animated: true, completion: nil)
}
#IBAction func onTappedPlusButton(sender: UIBarButtonItem)
{
let alert = UIAlertController(title: "Add Pro", message: nil, preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler
{ (textField) -> Void in
textField.placeholder = "Add Pro Here"
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alert.addAction(cancelAction)
let addAction = UIAlertAction(title: "Add", style: .Default) { (action) -> Void in
let addCollegesTextField = (alert.textFields?[0])! as UITextField
let 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, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == UITableViewCellEditingStyle.Delete
{
colleges.removeAtIndex(indexPath.row)
tableViewOutlet.reloadData()
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
If you want to implement all this in one view controller, you can try this:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == UITableViewCellEditingStyle.Delete
{
if tableView == tableViewOutlet
{
colleges.removeAtIndex(indexPath.row)
tableView.reloadData()
}
else
{
collegesTwo.removeAtIndex(indexPath.row)
tableView.reloadData()
}
}
}
But in this case better solution would be to create two classes called like DataSourceOne, DataSourceTwo (or TableViewModelOne, TableViewModelTwo), and implement all related logic there. This even could be two instances of just one class DataSource, depending on what exactly you need. Then you can instantiate those helper classes in viewDidLoad and assign them to dataSource and delegate properties of your table views. Your will also need to hold strong reference for them somewhere, because dataSource and delegate properties are week.

Resources