Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // Custom Cell
- //
- // Created by Tops on 7/6/17.
- // Copyright © 2017 Tops. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
- var name=[""]
- var city=[""]
- var img1=[""]
- override func viewDidLoad() {
- super.viewDidLoad()
- name = ["Dipak"]
- city = ["Jamnagar"]
- img1 = ["1"]
- // Do any additional setup after loading the view, typically from a nib.
- }
- override func viewWillAppear(_ animated: Bool)
- {
- navigationController?.isNavigationBarHidden = true
- table.reloadData()
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
- {
- return name.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
- {
- var cell = FirstTableViewCell()
- cell=tableView.dequeueReusableCell(withIdentifier: "cell 1") as! FirstTableViewCell
- cell.lblnm.text=name[indexPath.row]
- cell.lblcity.text=city[indexPath.row]
- cell.img.image=UIImage(named: img1[indexPath.row])
- cell.accessoryType=UITableViewCellAccessoryType.detailButton
- return cell
- }
- func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath)
- {
- let alert = UIAlertController.init(title:"Details Selected", message:"\n Your Seleted Name Is \( name[indexPath.row]) \n And City Is \(city[indexPath.row])", preferredStyle: .alert)
- let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
- alert.addAction(ok)
- present(alert, animated: true, completion: nil)
- }
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
- {
- let nxt=storyboard?.instantiateViewController(withIdentifier: "select") as! selectViewController
- nxt.strimg=UIImage(named: img1[indexPath.row])!
- nxt.strnm=name[indexPath.row]
- nxt.strcity=city[indexPath.row]
- navigationController?.pushViewController(nxt, animated: true)
- }
- // Deleted Butten Method
- /*
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
- {
- img1.remove(at: indexPath.row)
- name.remove(at: indexPath.row)
- city.remove(at: indexPath.row)
- table.reloadData()
- }
- */
- func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
- {
- let delete = UITableViewRowAction(style: .default, title: "Delete", handler: {ACTION in
- let con = UIAlertController.init(title:"Confirmation", message:"\n Are You Sure \n You Want To Delete ", preferredStyle: .alert)
- let dele = UIAlertAction(title: "Delete", style: .cancel, handler: {ACTION in
- self.img1.remove(at: indexPath.row)
- self.name.remove(at: indexPath.row)
- self.city.remove(at: indexPath.row)
- self.table.reloadData()
- })
- let can = UIAlertAction(title: "Cancel", style: .default, handler: nil)
- con.addAction(dele)
- con.addAction(can)
- self.present(con, animated: true, completion: nil)
- })
- let update = UITableViewRowAction(style: .normal, title: "Update", handler: {ACTION in
- let alrt = UIAlertController.init(title:"Seleted Item", message:"\n Your Seleted Name Is \( self.name[indexPath.row]) \n And City Is \(self.city[indexPath.row])", preferredStyle: .alert)
- alrt.addTextField(configurationHandler: { ACTION in
- ACTION.text = self.name[indexPath.row]
- ACTION.textAlignment=NSTextAlignment.center
- ACTION.borderStyle=UITextBorderStyle.bezel
- })
- alrt.addTextField(configurationHandler: {ACTION in
- ACTION.text = self.city[indexPath.row]
- ACTION.borderStyle=UITextBorderStyle.bezel
- ACTION.textAlignment=NSTextAlignment.center
- })
- let ok = UIAlertAction(title: "Ok", style: .default, handler: {ACTION in
- self.name[indexPath.row]=(alrt.textFields?[0].text)!
- self.city[indexPath.row]=(alrt.textFields?[1].text)!
- // self.name.remove(at: indexPath.row)
- // self.city.remove(at: indexPath.row)
- // elf.name.insert((alrt.textFields?[0].text)!, at: indexPath.row)
- // self.city.insert((alrt.textFields?[1].text)!, at: indexPath.row)
- self.table.reloadData()
- })
- alrt.addAction(ok)
- self.present(alrt, animated: true, completion: nil)
- })
- delete.backgroundColor=UIColor.darkGray
- update.backgroundColor=UIColor.orange
- return [delete,update]
- }
- @IBAction func btnadd(_ sender: UIButton)
- {
- let alrt = UIAlertController.init(title:"Add Item", message:"\n Enter Name \n And City ", preferredStyle: .alert)
- alrt.addTextField(configurationHandler: { ACTION in
- ACTION.placeholder="Enter Name"
- ACTION.textAlignment=NSTextAlignment.center
- ACTION.borderStyle=UITextBorderStyle.bezel
- })
- alrt.addTextField(configurationHandler: {ACTION in
- ACTION.placeholder="Enter City"
- ACTION.borderStyle=UITextBorderStyle.bezel
- ACTION.textAlignment=NSTextAlignment.center
- })
- let ok = UIAlertAction(title: "Ok", style: .default, handler: {ACTION in
- self.name.append((alrt.textFields?[0].text)!)
- self.city.append((alrt.textFields?[1].text)!)
- let ims = ["1","2","3","5","5","6","7","8","9","10","11"]
- let random = Int(arc4random_uniform(UInt32(ims.count)))
- self.img1.append(String(random))
- self.table.reloadData()
- })
- alrt.addAction(ok)
- self.present(alrt, animated: true, completion: nil)
- }
- @IBOutlet weak var table: UITableView!
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- }
- selectViewController.swift
- import UIKit
- class selectViewController: UIViewController {
- var strimg = UIImage()
- var strnm = ""
- var strcity = ""
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- override func viewWillAppear(_ animated: Bool)
- {
- img1.image=strimg
- lblnm.text="Name Is \(strnm)"
- lblcity.text="City Is \(strcity)"
- navigationController?.isNavigationBarHidden = true
- }
- @IBAction func btnback(_ sender: UIButton)
- {
- _ = navigationController?.popToRootViewController(animated: true)
- }
- @IBOutlet weak var img1: UIImageView!
- @IBOutlet weak var lblnm: UILabel!
- @IBOutlet weak var lblcity: UILabel!
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destinationViewController.
- // Pass the selected object to the new view controller.
- }
- */
- }
- FirstTableViewCell.swift
- import UIKit
- class FirstTableViewCell: UITableViewCell {
- override func awakeFromNib() {
- super.awakeFromNib()
- // Initialization code
- }
- @IBOutlet weak var lblcity: UILabel!
- @IBOutlet weak var lblnm: UILabel!
- @IBOutlet weak var img: UIImageView!
- override func setSelected(_ selected: Bool, animated: Bool) {
- super.setSelected(selected, animated: animated)
- // Configure the view for the selected state
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement