Using swipe gesture to edit table view cells in swift3 - swift3

How can I use Swip gesture to do "edit" and "delete" action on Each table view cell? Something like the email app! Any suggestion?

func tableView(_ tableView: UITableView, canEditRowAt indexPath:
IndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
func tableView(_ tableView: UITableView, commitEditingStyle
editingStyle: UITableViewCellEditingStyle, forRowAt indexPath:
IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .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
}
}

Related

Best way To load more data from API when scrolling to bottom as pagination

I want when scrolling to bottom in UITableView Fetch more date from API based on Page Number
Are use this method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (((scrollView.contentOffset.y + scrollView.frame.size.height) > scrollView.contentSize.height ) && ! isLoadingList){
self. isLoadingList = true
self. loadMoreItemsForList()
}
}
or used this method
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// fetch new data if user scroll to the last cell
guard isLoadingIndexPath(indexPath) else { return }
if self.totalItems > orders.count {
fetchNextPage()
}
}
use func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) method to fetch more data while scrolling to bottom.

why are the cells in my tableview resizing

I have a tableview with 2 cells. for some reason the cells get resized when I open in simulator. Trying to understand why. I added image of view.
this is the view code. for now i just wish the cells to get created at the height I set in IB
import UIKit
class userMenuViewController: BaseViewController,UITableViewDataSource,UITableViewDelegate {
var surveyNameArr = ["aaa","bbb","ccc"]
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (surveyNameArr.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! PrefCell2TableViewCell
return (cell)
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
addSlideMenuButton()
}
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.
}
*/
}
this is what i was looking for:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0;//Choose your custom row height
}

IOS - Capture event on button pressed and pass to tableview

I’m trying to capture an event from a button pressed so I can pass it to a table view, its a timer where I can capture the time and pass it to the tableview but I can't seem to be able to capture the event type.
In the example below I can capture the time a goal was scored but I need the words Goal also passing to the tableview
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
cell.backgroundColor = self.view.backgroundColor
**cell.textLabel?.text = "Help here to capture the button event"**
cell.detailTextLabel?.text = time[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return time.count
}
#IBAction func goalClicked(_ sender: UIButton) {
let stopWatchString = stopWatch.elapsedTimeSinceStart()
stopwatchLabel.text = stopWatchString
time.insert(stopWatchString, at: 0)
self.tableView.reloadData()
}
Thanks
Sorted it.
var event: String!
then in the tableView
cell.textLabel?.Text = event
Finally in the button function
event = "Goal"
tableView.reloadData()

Cannot deselect previous programmatically selected row in tableview after another selection

I am using both in cellForRowAt and exact in the same function that initialize the tableview this code:
cell.accessoryType = selectedOption == indexPath.row ? .checkmark : .none
and my tableview got checkmark automatically for selectedOption, but when I select another option the checkmark that I set earlier doesn't disapear and after that there are 2 checkmarks on the table with setted allowsMultipleSelection to false. Why this is happening and why my first checkmark doesn't hide after my selection ?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
}
}
I think if you reload your tableview it will works, your code is fine, just reload your table in didselect,
Thanks

UITableView allow multi selection when select cell more than one cell selected

I have UITableView allow multi selection for the user to select the languages he speaks when select cell and scroll down in the table other cells selected.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell!
cell = tableView.dequeueReusableCell(withIdentifier: "language",for: indexPath)
cell?.textLabel?.text = languageValues[indexPath.row]
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .none
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
}
gif explain the problem
Your problem is that cells get cached and reused. This means that they are a bad place to store state information.
I suggest that your languageValues array should contain objects where each object has a name string and a selected boolean. When someone selects or deselects a cell, change the boolean to match and reload data for the affected cell (or the table).
That way only cellForRowAt indexPath will change the accessoryType value.