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

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.

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
}

How to fix image scaling when set from Swift code

I'm trying to set images from my Swift code and display them in table view cells but when I set them from Swift code they are scaled or in totally different place.
Did anybody have similar issue?
My code:
var datasources = ["mapmyrun","fitbitlogo"]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableview.dequeueReusableCell(withIdentifier: "datasourcecell", for: indexPath) as? DataSourceCell else{
fatalError("cell is not an instance of DataSourceCell")
}
cell.imageView?.contentMode = UIViewContentMode.scaleAspectFit
let image = UIImage(named: datasources[indexPath.row])
cell.imageView?.image = image
return cell
}
Result from code:
Result when I set it manually. It is perfect fit:

Using swipe gesture to edit table view cells in 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
}
}

indexPath.row goes from 0 to 16 and not beyond

why is the code below only displays cells from 1 to 17 ?
internal func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return 20
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier:"Cell")
cell.textLabel?.text=String(indexPath.row+1)
print(indexPath.row)
return cell
}`
Output :
Please enable scrolling of your tableview, and scroll down cell in your output.