Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // Custom Cell
  4. //
  5. // Created by Tops on 7/6/17.
  6. // Copyright © 2017 Tops. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  12.  
  13. var name=[""]
  14. var city=[""]
  15. var img1=[""]
  16.  
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19.  
  20. name = ["Dipak"]
  21. city = ["Jamnagar"]
  22. img1 = ["1"]
  23.  
  24. // Do any additional setup after loading the view, typically from a nib.
  25. }
  26. override func viewWillAppear(_ animated: Bool)
  27. {
  28.  
  29. navigationController?.isNavigationBarHidden = true
  30. table.reloadData()
  31. }
  32.  
  33. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  34. {
  35. return name.count
  36. }
  37.  
  38. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
  39. {
  40. var cell = FirstTableViewCell()
  41.  
  42. cell=tableView.dequeueReusableCell(withIdentifier: "cell 1") as! FirstTableViewCell
  43. cell.lblnm.text=name[indexPath.row]
  44. cell.lblcity.text=city[indexPath.row]
  45. cell.img.image=UIImage(named: img1[indexPath.row])
  46.  
  47. cell.accessoryType=UITableViewCellAccessoryType.detailButton
  48.  
  49. return cell
  50. }
  51.  
  52. func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath)
  53. {
  54. 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)
  55. let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
  56.  
  57. alert.addAction(ok)
  58.  
  59. present(alert, animated: true, completion: nil)
  60. }
  61.  
  62. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
  63. {
  64. let nxt=storyboard?.instantiateViewController(withIdentifier: "select") as! selectViewController
  65.  
  66. nxt.strimg=UIImage(named: img1[indexPath.row])!
  67. nxt.strnm=name[indexPath.row]
  68. nxt.strcity=city[indexPath.row]
  69. navigationController?.pushViewController(nxt, animated: true)
  70. }
  71.  
  72. // Deleted Butten Method
  73.  
  74. /*
  75.  
  76. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
  77. {
  78. img1.remove(at: indexPath.row)
  79. name.remove(at: indexPath.row)
  80. city.remove(at: indexPath.row)
  81.  
  82. table.reloadData()
  83. }
  84.  
  85. */
  86.  
  87. func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
  88. {
  89. let delete = UITableViewRowAction(style: .default, title: "Delete", handler: {ACTION in
  90.  
  91. let con = UIAlertController.init(title:"Confirmation", message:"\n Are You Sure \n You Want To Delete ", preferredStyle: .alert)
  92.  
  93. let dele = UIAlertAction(title: "Delete", style: .cancel, handler: {ACTION in
  94.  
  95. self.img1.remove(at: indexPath.row)
  96. self.name.remove(at: indexPath.row)
  97. self.city.remove(at: indexPath.row)
  98. self.table.reloadData()
  99. })
  100. let can = UIAlertAction(title: "Cancel", style: .default, handler: nil)
  101.  
  102. con.addAction(dele)
  103. con.addAction(can)
  104.  
  105. self.present(con, animated: true, completion: nil)
  106.  
  107. })
  108. let update = UITableViewRowAction(style: .normal, title: "Update", handler: {ACTION in
  109.  
  110. 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)
  111.  
  112. alrt.addTextField(configurationHandler: { ACTION in
  113.  
  114. ACTION.text = self.name[indexPath.row]
  115. ACTION.textAlignment=NSTextAlignment.center
  116. ACTION.borderStyle=UITextBorderStyle.bezel
  117.  
  118. })
  119. alrt.addTextField(configurationHandler: {ACTION in
  120.  
  121. ACTION.text = self.city[indexPath.row]
  122. ACTION.borderStyle=UITextBorderStyle.bezel
  123. ACTION.textAlignment=NSTextAlignment.center
  124.  
  125. })
  126.  
  127.  
  128. let ok = UIAlertAction(title: "Ok", style: .default, handler: {ACTION in
  129.  
  130.  
  131. self.name[indexPath.row]=(alrt.textFields?[0].text)!
  132. self.city[indexPath.row]=(alrt.textFields?[1].text)!
  133.  
  134. // self.name.remove(at: indexPath.row)
  135. // self.city.remove(at: indexPath.row)
  136. // elf.name.insert((alrt.textFields?[0].text)!, at: indexPath.row)
  137. // self.city.insert((alrt.textFields?[1].text)!, at: indexPath.row)
  138.  
  139. self.table.reloadData()
  140.  
  141.  
  142. })
  143.  
  144. alrt.addAction(ok)
  145.  
  146. self.present(alrt, animated: true, completion: nil)
  147.  
  148. })
  149.  
  150. delete.backgroundColor=UIColor.darkGray
  151. update.backgroundColor=UIColor.orange
  152.  
  153. return [delete,update]
  154. }
  155. @IBAction func btnadd(_ sender: UIButton)
  156. {
  157. let alrt = UIAlertController.init(title:"Add Item", message:"\n Enter Name \n And City ", preferredStyle: .alert)
  158.  
  159. alrt.addTextField(configurationHandler: { ACTION in
  160.  
  161. ACTION.placeholder="Enter Name"
  162. ACTION.textAlignment=NSTextAlignment.center
  163. ACTION.borderStyle=UITextBorderStyle.bezel
  164.  
  165. })
  166. alrt.addTextField(configurationHandler: {ACTION in
  167.  
  168. ACTION.placeholder="Enter City"
  169. ACTION.borderStyle=UITextBorderStyle.bezel
  170. ACTION.textAlignment=NSTextAlignment.center
  171.  
  172. })
  173.  
  174.  
  175. let ok = UIAlertAction(title: "Ok", style: .default, handler: {ACTION in
  176.  
  177.  
  178. self.name.append((alrt.textFields?[0].text)!)
  179. self.city.append((alrt.textFields?[1].text)!)
  180. let ims = ["1","2","3","5","5","6","7","8","9","10","11"]
  181. let random = Int(arc4random_uniform(UInt32(ims.count)))
  182.  
  183. self.img1.append(String(random))
  184.  
  185. self.table.reloadData()
  186. })
  187.  
  188. alrt.addAction(ok)
  189.  
  190. self.present(alrt, animated: true, completion: nil)
  191. }
  192.  
  193. @IBOutlet weak var table: UITableView!
  194. override func didReceiveMemoryWarning() {
  195. super.didReceiveMemoryWarning()
  196. // Dispose of any resources that can be recreated.
  197. }
  198.  
  199.  
  200. }
  201.  
  202.  
  203.  
  204.  
  205.  
  206. selectViewController.swift
  207.  
  208.  
  209.  
  210. import UIKit
  211.  
  212. class selectViewController: UIViewController {
  213. var strimg = UIImage()
  214. var strnm = ""
  215. var strcity = ""
  216.  
  217. override func viewDidLoad() {
  218. super.viewDidLoad()
  219.  
  220. // Do any additional setup after loading the view.
  221. }
  222.  
  223. override func viewWillAppear(_ animated: Bool)
  224. {
  225. img1.image=strimg
  226. lblnm.text="Name Is \(strnm)"
  227. lblcity.text="City Is \(strcity)"
  228. navigationController?.isNavigationBarHidden = true
  229. }
  230.  
  231. @IBAction func btnback(_ sender: UIButton)
  232. {
  233. _ = navigationController?.popToRootViewController(animated: true)
  234. }
  235.  
  236. @IBOutlet weak var img1: UIImageView!
  237. @IBOutlet weak var lblnm: UILabel!
  238. @IBOutlet weak var lblcity: UILabel!
  239. override func didReceiveMemoryWarning() {
  240. super.didReceiveMemoryWarning()
  241. // Dispose of any resources that can be recreated.
  242. }
  243.  
  244.  
  245. /*
  246. // MARK: - Navigation
  247.  
  248. // In a storyboard-based application, you will often want to do a little preparation before navigation
  249. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  250. // Get the new view controller using segue.destinationViewController.
  251. // Pass the selected object to the new view controller.
  252. }
  253. */
  254.  
  255. }
  256.  
  257.  
  258. FirstTableViewCell.swift
  259.  
  260. import UIKit
  261.  
  262. class FirstTableViewCell: UITableViewCell {
  263.  
  264. override func awakeFromNib() {
  265. super.awakeFromNib()
  266. // Initialization code
  267. }
  268.  
  269. @IBOutlet weak var lblcity: UILabel!
  270. @IBOutlet weak var lblnm: UILabel!
  271. @IBOutlet weak var img: UIImageView!
  272. override func setSelected(_ selected: Bool, animated: Bool) {
  273. super.setSelected(selected, animated: animated)
  274.  
  275. // Configure the view for the selected state
  276. }
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement