UICollection load more cell manually Swift - ios

I have made a UICollection who contain only 2 cells and with a footer who contain a button "Show me more". And when we click on the button "Show me more" it should append 2 more cells in the UICollection.
My problem is when I click on the button the UICollection doesn't reload and append the 2 new cells. I checked different post about infinite scrolling, but I can't resolve my problem. Here is my code:
My button and his function
lazy var showMoreButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Show me more", for: .normal)
button.addTarget(self, action: #selector(handleShowMore), for: .touchUpInside)
return button
}()
func handleShowMore(){
homeDatasource?.incrementNumberOfUser()
HomeDatasourceController().collectionView?.reloadData()
}
My collection
`
var numberOfUser: Int = 2
class HomeDatasource: Datasource, JSONDecodable {
override func footerClasses() -> [DatasourceCell.Type]? {
return [UserFooter.self]
}
override func cellClasses() -> [DatasourceCell.Type] {
return [UserCell.self, TweetCell.self] //UserCell -> section 0 and TweetCell -> section 1
}
override func item(_ indexPath: IndexPath) -> Any? {
if indexPath.section == 1 {
return tweets[indexPath.item]
}
return users[indexPath.item]
}
override func numberOfItems(_ section: Int) -> Int {
if section == 1 {
return tweets.count
}
return numberOfUser //users.count
}
override func numberOfSections() -> Int {
return 2
}
func incrementNumberOfUser() {
if numberOfUser <= users.count {
numberOfUser = numberOfUser + 2
}
}
}
`

this line
HomeDatasourceController().collectionView?.reloadData()
seems to be the cause. Because you are creating a new 'HomeDatasourceController' although at the time of reloading it should already exist. So you should reuse the already existing instance of 'HomeDatasourceController'.
Somewhere you created an instance e.g.
let datasourceController = HomeDatasourceController()
now reuse that instance in
func handleShowMore() {
homeDatasource?.incrementNumberOfUser()
datasourceController.collectionView?.reloadData()
}

Related

Swift expandable table view header for sections problem [duplicate]

This question already has answers here:
How to deal with dynamic Sections and Rows in iOS UITableView
(3 answers)
Closed 3 years ago.
I have question about expandable table view by header. I made it for section zero and its working correctly but when I try to make it same thing for section one too it gives error which is
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
If I didn't trigger second header function, first one still working. When I clicked second header ( which is section one) it gives that error.
Here my header view code:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let button = UIButton(type: .system)
button.setTitle("Kapat", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = .lightGray
button.titleLabel!.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(handleExpandCloseForAlim(sender:)), for: .touchUpInside)
return button
}
if section == 1 {
let button2 = UIButton(type: .system)
button2.setTitle("Kapat", for: .normal)
button2.setTitleColor(.black, for: .normal)
button2.backgroundColor = .lightGray
button2.titleLabel!.font = UIFont.boldSystemFont(ofSize: 14)
button2.addTarget(self, action: #selector(handleExpandCloseForTeslim(sender:)), for: .touchUpInside)
return button2
}
else{
return nil
}
}
Here my action functions:
#objc func handleExpandCloseForAlim(sender: UIButton) {
var indexPaths = [IndexPath]()
for row in self.userAdressDict.indices{
let indexPath = IndexPath(row: row, section: 0)
indexPaths.append(indexPath)
}
let indexPath = IndexPath(row: (sender.tag), section: 0)
let adresso = self.userAdressDict[indexPath.row]
let isExp = adresso.isExpanded
adresso.isExpanded = !isExp!
if isExp! {
tableView.deleteRows(at: indexPaths, with: .fade)
}else {
tableView.insertRows(at: indexPaths, with: .fade)
}
print(adresso.isExpanded!, adresso.userMahalle!)
}
#objc func handleExpandCloseForTeslim(sender: UIButton) {
var indexPathsForTeslim = [IndexPath]()
for row in self.userAdressDictForTeslim.indices{
let indexPath = IndexPath(row: row, section: 1)
indexPathsForTeslim.append(indexPath)
}
let indexPath = IndexPath(row: (sender.tag), section: 1)
let adresso = self.userAdressDictForTeslim[indexPath.row]
let isExp = adresso.isExpanded
adresso.isExpanded = !isExp!
if isExp! {
tableView.deleteRows(at: indexPathsForTeslim, with: .fade)
}else {
tableView.insertRows(at: indexPathsForTeslim, with: .fade)
}
print(adresso.isExpanded!, adresso.userMahalle!)
}
And here my numberOfRowsInSection part:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
guard let element = self.userAdressDict.first as? adresStruct else { return 0 }
if element.isExpanded {
return self.userAdressDict.count
}
else {
return 0
}
}
if section == 1 {
guard let teslim = self.userAdressDictForTeslim.last as? adresStruct else { return 0 }
if teslim.isExpanded {
return self.userAdressDictForTeslim.count
}
else {
return 0
}
}
return 1
}
I can't find any solution. I assume sender thing working wrongly but it may be another problem. I hope I asked clearly.Please help me.
I am not deep dive in your code but I copy-paste into an Xcode project to look into. So there is basic way to solve your problem, I hope it helps to you.
I assume your class is like that.
class Adres{
var title: String = ""
var isExpanded: Bool = true
init(title: String, isExpanded: Bool){
self.title = title
self.isExpanded = isExpanded
}
}
And in there mainly 2 different variable for adresDict & adresDictForTeslim.
So I keep an lock array to doing logic stuff of expandable.
var keys: [Int] = [1,1]
<1> Show
<0> Hide
Now, the arrays elements are showing into UITableView, because its expanded.
There's mock data.
var userAdressDict = [Adres(title: "First Adres", isExpanded: true) ,Adres(title: "Second Adres", isExpanded: true)]
var userAdressDictForTeslim = [Adres(title: "First Teslim", isExpanded: true),Adres(title: "Second Teslim", isExpanded: true)]
viewForHeaderInSection is same as well.
And for numberOfRowsInSection check array for key that if its expanded or not.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if self.keys[0] == 1 {
return userAdressDict.count
}else{
return 0
}
}else{
if self.keys[1] == 1 {
return userAdressDictForTeslim.count
}else{
return 0
}
}
}
Then check these keys in handler functions.
#objc func handleExpandCloseForAlim(sender: UIButton) {
if keys[0] == 0 {
keys[0] = 1
}else{
keys[0] = 0
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
If first array elements in table view is expanded close it, or not expand it.
Then in another function.
#objc func handleExpandCloseForTeslim(sender: UIButton) {
if keys[1] == 0 {
keys[1] = 1
}else{
keys[1] = 0
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
So far so good. It's working on my simulator.
Then there's more efficient way to handle keys of expanded.
Make your UIButton's of the sectionHeader as public variable in your ViewController and control it if it's title is "Kapat" and clicked then it must be "Aç" or if its "Aç" and touched it must be "Kapat".

Properly delegate button action from custom Cell to delete rows in UITableView

Still very much a Swift noob, I have been looking around for a proper way/best practice to manage row deletions in my UITableView (which uses custom UserCells) based on tapping a UIButton inside the UserCell using delegation which seems to be the cleanest way to do it.
I followed this example: UITableViewCell Buttons with action
What I have
UserCell class
protocol UserCellDelegate {
func didPressButton(_ tag: Int)
}
class UserCell: UITableViewCell {
var delegate: UserCellDelegate?
let addButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Add +", for: .normal)
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
addSubview(addButton)
addButton.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -6).isActive = true
addButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
addButton.heightAnchor.constraint(equalToConstant: self.frame.height / 2).isActive = true
addButton.widthAnchor.constraint(equalToConstant: self.frame.width / 6).isActive = true
}
func buttonPressed(_ sender: UIButton) {
delegate?.didPressButton(sender.tag)
}
}
TableViewController class:
class AddFriendsScreenController: UITableViewController, UserCellDelegate {
let cellId = "cellId"
var users = [User]()
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell
cell.delegate = self
cell.tag = indexPath.row
return cell
}
func didPressButton(_ tag: Int) {
let indexPath = IndexPath(row: tag, section: 0)
users.remove(at: tag)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
where the Users in users are appended with a call to the database in the view controller.
My issues
The button in each row of the Table View is clickable but does not do anything
The button seems to be clickable only when doing a "long press", i.e. finger stays on it for a ~0.5s time
Will this method guarantee that the indexPath is updated and will not fall out of scope ? I.e. if a row is deleted at index 0, will deleting the "new" row at index 0 work correctly or will this delete the row at index 1 ?
What I want
Being able to click the button in each row of the table, which would remove it from the tableview.
I must be getting something rather basic wrong and would really appreciate if a Swift knight could enlighten me.
Many thanks in advance.
There are at least 3 issues in your code:
In UserCell you should call:
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
once your cell has been instantiated (say, from your implementation of init(style:reuseIdentifier:)) so that self refers to an actual instance of UserCell.
In AddFriendsScreenController's tableView(_:cellForRowAt:) you are setting the tag of the cell itself (cell.tag = indexPath.row) but in your UserCell's buttonPressed(_:) you are using the tag of the button. You should modify that function to be:
func buttonPressed(_ sender: UIButton) {
//delegate?.didPressButton(sender.tag)
delegate?.didPressButton(self.tag)
}
As you guessed and as per Prema Janoti's answer you ought to reload you table view once you deleted a row as your cells' tags will be out of sync with their referring indexPaths. Ideally you should avoid relying on index paths to identify cells but that's another subject.
EDIT:
A simple solution to avoid tags being out of sync with index paths is to associate each cell with the User object they are supposed to represent:
First add a user property to your UserCell class:
class UserCell: UITableViewCell {
var user = User() // default with a dummy user
/* (...) */
}
Set this property to the correct User object from within tableView(_:cellForRowAt:):
//cell.tag = indexPath.row
cell.user = self.users[indexPath.row]
Modify the signature of your UserCellDelegate protocol method to pass the user property stored against the cell instead of its tag:
protocol UserCellDelegate {
//func didPressButton(_ tag: Int)
func didPressButtonFor(_ user: User)
}
Amend UserCell's buttonPressed(_:) action accordingly:
func buttonPressed(_ sender: UIButton) {
//delegate?.didPressButton(sender.tag)
//delegate?.didPressButton(self.tag)
delegate?.didPressButtonFor(self.user)
}
Finally, in your AddFriendsScreenController, identify the right row to delete based on the User position in the data source:
//func didPressButton(_ tag: Int) { /* (...) */ } // Scrap this.
func didPressButtonFor(_ user: User) {
if let index = users.index(where: { $0 === user }) {
let indexPath = IndexPath(row: index, section: 0)
users.remove(at: index)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
Note the if let index = ... construct (optional binding) and the triple === (identity operator).
This downside of this approach is that it will create tight coupling between your User and UserCell classes. Best practice would dictate using a more complex MVVM pattern for example, but that really is another subject...
There is a lot of bad/old code on the web, even on SO. What you posted has "bad practice" written all over it. So first a few pointers:
Avoid an UITableViewController at all cost. Have a normal view controller with a table view on it
Delegates should always be weak unless you are 100% sure what you are doing
Be more specific when naming protocols and protocol methods
Keep everything private if possible, if not then use fileprivate. Only use the rest if you are 100% sure it is a value you want to expose.
Avoid using tags at all cost
The following is an example of responsible table view with a single cell type which has a button that removes the current cell when pressed. The whole code can be pasted into your initial ViewController file when creating a new project. In storyboard a table view is added constraint left, right, top, bottom and an outlet to the view controller. Also a cell is added in the table view with a button in it that has an outlet to the cell MyTableViewCell and its identifier is set to "MyTableViewCell".
The rest should be explained in the comments.
class ViewController: UIViewController {
#IBOutlet private weak var tableView: UITableView? // By default use private and optional. Always. For all outlets. Only expose it if you really need it outside
fileprivate var myItems: [String]? // Use any objects you need.
override func viewDidLoad() {
super.viewDidLoad()
// Attach table viw to self
tableView?.delegate = self
tableView?.dataSource = self
// First refresh and reload the data
refreshFromData() // This is to ensure no defaults are visible in the beginning
reloadData()
}
private func reloadData() {
myItems = nil
// Simulate a data fetch
let queue = DispatchQueue(label: "test") // Just for the async example
queue.async {
let items: [String] = (1...100).flatMap { "Item: \($0)" } // Just generate some string
Thread.sleep(forTimeInterval: 3.0) // Wait 3 seconds
DispatchQueue.main.async { // Go back to main thread
self.myItems = items // Assign data source to self
self.refreshFromData() // Now refresh the table view
}
}
}
private func refreshFromData() {
tableView?.reloadData()
tableView?.isHidden = myItems == nil
// Add other stuff that need updating here if needed
}
/// Will remove an item from the data source and update the array
///
/// - Parameter item: The item to remove
fileprivate func removeItem(item: String) {
if let index = myItems?.index(of: item) { // Get the index of the object
tableView?.beginUpdates() // Begin updates so the table view saves the current state
myItems = myItems?.filter { $0 != item } // Update our data source first
tableView?.deleteRows(at: [IndexPath(row: index, section: 0)], with: .fade) // Do the table view cell modifications
tableView?.endUpdates() // Commit the modifications
}
}
}
// MARK: - UITableViewDelegate, UITableViewDataSource
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myItems?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as? MyTableViewCell {
cell.item = myItems?[indexPath.row]
cell.delegate = self
return cell
} else {
return UITableViewCell()
}
}
}
// MARK: - MyTableViewCellDelegate
extension ViewController: MyTableViewCellDelegate {
func myTableViewCell(pressedMainButton sender: MyTableViewCell) {
guard let item = sender.item else {
return
}
// Delete the item if main button is pressed
removeItem(item: item)
}
}
protocol MyTableViewCellDelegate: class { // We need ": class" so the delegate can be marked as weak
/// Called on main button pressed
///
/// - Parameter sender: The sender cell
func myTableViewCell(pressedMainButton sender: MyTableViewCell)
}
class MyTableViewCell: UITableViewCell {
#IBOutlet private weak var button: UIButton?
weak var delegate: MyTableViewCellDelegate? // Must be weak or we can have a retain cycle and create a memory leak
var item: String? {
didSet {
button?.setTitle(item, for: .normal)
}
}
#IBAction private func buttonPressed(_ sender: Any) {
delegate?.myTableViewCell(pressedMainButton: self)
}
}
In your case the String should be replaced by the User. Next to that you will have a few changes such as the didSet in the cell (button?.setTitle(item.name, for: .normal) for instance) and the filter method should use === or compare some id or something.
try this -
update didPressButton method like below -
func didPressButton(_ tag: Int) {
let indexPath = IndexPath(row: tag, section: 0)
users.remove(at: tag)
tableView.reloadData()
}

iOS Swift, Update UITableView custom cell label outside of tableview CellForRow using tag

Setup (Swift 1.2 / iOS 8.4):
I have UITableView custom cell (identifier = Cell) inside UIViewController. Have two buttons (increment/decrement count) and a label (display count) inside the custom TableView cell.
Goal:
Update the label as we press the increase count or decrease count button.
At present I am able to get the button Tag and call a function outside of the CellForRowAtIndexPath. The button press increases and decreases the count. But I am not able to display the count update in the label.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:FoodTypeTableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FoodTypeTableViewCell
cell.addBtn.tag = indexPath.row // Button 1
cell.addBtn.addTarget(self, action: "addBtn:", forControlEvents: .TouchUpInside)
cell.subBtn.tag = indexPath.row // Button 2
cell.subBtn.addTarget(self, action: "subBtn:", forControlEvents: .TouchUpInside)
cell.countLabel.text = // How can I update this label
return cell
}
func addBtn(sender: AnyObject) -> Int {
let button: UIButton = sender as! UIButton
count = 1 + count
println(count)
return count
}
func subBtn(sender: AnyObject) -> Int {
let button: UIButton = sender as! UIButton
if count == 0 {
println("Count zero")
} else {
count = count - 1
}
println(count)
return count
}
I have seen this question here and there but was not able to find a clear answer in Swift. I would really appreciate if you could help answer it clearly so that other people can not just copy, but clearly understand what is going on.
Thank you.
Here is a solution that doesn't require tags. I'm not going to recreate the cell exactly as you want, but this covers the part you are asking about.
Using Swift 2 as I don't have Xcode 6.x anymore.
Let's start with the UITableViewCell subclass. This is just a dumb container for a label that has two buttons on it. The cell doesn't actually perform any specific button actions, it just passes on the call to closures that are provided in the configuration method. This is part of MVC. The view doesn't interact with the model, just the controller. And the controller provides the closures.
import UIKit
typealias ButtonHandler = (Cell) -> Void
class Cell: UITableViewCell {
#IBOutlet private var label: UILabel!
#IBOutlet private var addButton: UIButton!
#IBOutlet private var subtractButton: UIButton!
var incrementHandler: ButtonHandler?
var decrementHandler: ButtonHandler?
func configureWithValue(value: UInt, incrementHandler: ButtonHandler?, decrementHandler: ButtonHandler?) {
label.text = String(value)
self.incrementHandler = incrementHandler
self.decrementHandler = decrementHandler
}
#IBAction func increment(sender: UIButton) {
incrementHandler?(self)
}
#IBAction func decrement(sender: UIButton) {
decrementHandler?(self)
}
}
Now the controller is just as simple
import UIKit
class ViewController: UITableViewController {
var data: [UInt] = Array(count: 20, repeatedValue: 0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell
cell.configureWithValue(data[indexPath.row], incrementHandler: incrementHandler(), decrementHandler: decrementHandler())
return cell
}
private func incrementHandler() -> ButtonHandler {
return { [unowned self] cell in
guard let row = self.tableView.indexPathForCell(cell)?.row else { return }
self.data[row] = self.data[row] + UInt(1)
self.reloadCellAtRow(row)
}
}
private func decrementHandler() -> ButtonHandler {
return { [unowned self] cell in
guard
let row = self.tableView.indexPathForCell(cell)?.row
where self.data[row] > 0
else { return }
self.data[row] = self.data[row] - UInt(1)
self.reloadCellAtRow(row)
}
}
private func reloadCellAtRow(row: Int) {
let indexPath = NSIndexPath(forRow: row, inSection: 0)
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
tableView.endUpdates()
}
}
When the cell is dequeued, it configures the cell with the value to show in the label and provides the closures that handle the button actions. These controllers are what interact with the model to increment and decrement the values that are being displayed. After changing the model, it reloads the changed cell in the tableview.
The closure methods take a single parameter, a reference to the cell, and from this it can find the row of the cell. This is a lot more de-coupled than using tags, which are a very brittle solution to knowing the index of a cell in a tableview.
You can download a full working example (Requires Xcode7) from https://bitbucket.org/abizern/so-32931731/get/ce31699d92a5.zip
I have never seen anything like this before so I am not sure if this will be the correct way to do. But I got the intended functionality using the bellow code:
For people who find it difficult to understand:
The only problem we have in this is to refer to the TableView Cell. Once you figure out a way to refer the cell, you can interact with the cell components.
func addBtn(sender: AnyObject) -> Int {
let button: UIButton = sender as! UIButton
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0) // This defines what indexPath is which is used later to define a cell
let cell = tableView.cellForRowAtIndexPath(indexPath) as! FoodTypeTableViewCell! // This is where the magic happens - reference to the cell
count = 1 + count
println(count)
cell.countLabel.text = "\(count)" // Once you have the reference to the cell, just use the traditional way of setting up the objects inside the cell.
return count
}
func subBtn(sender: AnyObject) -> Int {
let button: UIButton = sender as! UIButton
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! FoodTypeTableViewCell!
if count == 0 {
println("Count zero")
} else {
count = count - 1
}
cell.countLabel.text = "\(count)"
println(count)
return count
}
I hope someone will benefit from this.
PLEASE CORRECT ME IF THERE IS SOME PROBLEM IN THIS SOLUTION OR THERE IS A BETTER/PROPER WAY TO DO THIS.
Use tableView.reloadData() to reload your tableView content each time you click a button.
let text = "something"
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:FoodTypeTableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FoodTypeTableViewCell
cell.addBtn.tag = indexPath.row // Button 1
cell.addBtn.addTarget(self, action: "addBtn:", forControlEvents: .TouchUpInside)
cell.subBtn.tag = indexPath.row // Button 2
cell.subBtn.addTarget(self, action: "subBtn:", forControlEvents: .TouchUpInside)
cell.countLabel.text = something
return cell
}
func addBtn(sender: AnyObject) -> Int {
let button: UIButton = sender as! UIButton
count = 1 + count
println(count)
something = "\(count)"
self.tableView.reloadData()
return count
}
func subBtn(sender: AnyObject) -> Int {
let button: UIButton = sender as! UIButton
if count == 0 {
println("Count zero")
} else {
count = count - 1
}
println(count)
something = "\(count)"
self.tableView.reloadData()
return count
}
Update1
After your comments ...
you have an array (one value for each food) like this, and whenever you click on a button, you take the index of the row the contains that button, then use that index to retrive the value of count from your array, then reload the table view content.

Selected button in cell is showing up on reuse cells - also using sender.tag for button distinction

I have a bunch of rows in a table and each row consists of 6 UIButtons (they are checkbox images). I started with just trying to get one button to work before linking all of them and that worked somewhat. The code would update the image to a checked box if I clicked on it and if I scrolled down and back up the first cell first box would be the only one checked. If I repeatedly scroll up and down though, it will randomly unselect that first cell first box. Here is my ViewController code:
//
// TestViewController.swift
//
import UIKit
import Parse
class TestViewController: UIViewController, UITableViewDelegate {
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! protoCell
configure(cell, forRowAtIndexPath: indexPath)
cell.customIndexPath = indexPath
return cell
}
func configure(cell: protoCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.selectedIndexPaths.containsObject(indexPath) {
println("Contains")
let image = UIImage(named: "checkedbox.png")
cell.button.setImage(image, forState: .Normal)
println(cell.selectedIndexPaths)
} else {
println("Not Contains")
let image = UIImage(named: "checkbox.png")
cell.button.setImage(image, forState: .Normal)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am controlling the table using a custom cocoa class for my UITableViewCell which has the code here:
//
// protoCell.swift
// ParseStarterProject
//
import UIKit
class protoCell: UITableViewCell {
var selectedIndexPaths = NSMutableSet()
var customIndexPath: NSIndexPath = NSIndexPath()
var isPressed = [1, 1, 1, 1, 1, 1]
#IBOutlet var button: UIButton!
#IBAction func buttonPressed(sender: AnyObject) {
println(isPressed[sender.tag])
if isPressed[sender.tag] == 1 {
isPressed[sender.tag] = 2
let image = UIImage(named: "checkedbox.png")
sender.setImage(image, forState: .Normal)
selectedIndexPaths.addObject(customIndexPath)
} else {
isPressed[sender.tag] = 1
let image = UIImage(named: "checkbox.png")
sender.setImage(image, forState: .Normal)
selectedIndexPaths.removeObject(customIndexPath)
}
println(isPressed)
}
}
I know the code is wrong because of what I am seeing. Also all images besides the first button are repeating. I am not too familiar with Objective-C so some of the searching has been hard.
Any help would be great!
Please see the skeleton code below (I just typed here, and not tested) that should give your a fair idea as to what needs be done-
class Config{
enum ButtonState : Int {
case NoneSelected,
case Button1Selected,
case Button2Selected,
case Button3Selected,
case Button4Selected,
case Button5Selected,
case Button6Selected
}
var buttonState : Int = . NoneSelected
}
//Within TableView controller, maintain an array of 10 such objects
var configs = [ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState()]
//This is how you need to configure the cell now-
func configure(cell: protoCell, forRowAtIndexPath indexPath: NSIndexPath) {
//First deselect all buttons
DeselectAllButtons()
//Get proper config object
var config = configs[indexPath.row]
//Maintain a ref to the config in the cell.
self.config = config
switch config. buttonState{
case . NoneSelected
DeselectAllButtons()
case let x where (x & Button1Selected) == true
SelectButton(button1)
case let x where (x & Button1Selected) == true
SelectButton(button1)
case let x where (x & Button2Selected) == true
SelectButton(button3)
case let x where (x & Button3Selected) == true
SelectButton(button3)
case let x where (x & Button4Selected) == true
SelectButton(button4)
case let x where (x & Button5Selected) == true
SelectButton(button5)
case let x where (x & Button1Selected) == true
SelectButton(button6)
default:
DeselectAllButtons()
}
}
//And your action in cell can be-
func buttonPressed(sender: AnyObject){
switch sender{
case button1
self.config.buttonState |= .Button1Selected
case button2
self.config.buttonState |= .Button2Selected
case button3
self.config.buttonState |= .Button3Selected
case button4
self.config.buttonState |= .Button4Selected
case button5
self.config.buttonState |= .Button5Selected
case button6
self.config.buttonState |= .Button6Selected
}
}

Cell reuse beginner? Tapping button in one cell affects another

I have a TableViewController with a custom cell. When I tap the like button inside one of the cells, it is causing at least one other cell to have the like button tapped.
I am using Parse, and it is not affecting the actual like count of the second one which is being ghost tapped, but it is disabling the like button and turning it red.
I have read about cell reuse, and similar topics but am completely lost. I am new to swift, and if someone could help me navigate how to fix this, I can't find a solution on SO about Swift, and Parse.
TableViewController
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:ChinTwoTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ChinTwoTableViewCell
cell.selectionStyle = .None
// Configure the cell...
let chinTwo:PFObject = self.timelineData.objectAtIndex(indexPath.row) as! PFObject
var myVar:Int = chinTwo.objectForKey("likeCount") as! Int
cell.countLabel.text = String(myVar)
cell.nameLabel.text = chinTwo.objectForKey("name") as? String
cell.bodyText.text = chinTwo.objectForKey("body") as! String
cell.bodyText.font = UIFont(name: "HelveticaNeue-UltraLight", size: 18)
cell.bodyText.textAlignment = .Center
cell.likeButton.tag = indexPath.row;
cell.likeButton.addTarget(self, action: "likeButtonTapped:", forControlEvents: .TouchUpInside)
return cell
}
#IBAction func likeButtonTapped(sender: AnyObject) {
let chinTwo = self.timelineData[sender.tag] as! PFObject
chinTwo["likeCount"] = (chinTwo["likeCount"] as! Int) + 1
sender.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
chinTwo.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
println("Worked")
} else {
println("Didn't Work")
}
}
self.tableView.reloadData()
}
TableViewCell
#IBAction func likeTapped(sender: AnyObject) {
likeButton.enabled = false
}
The same problem occurs with the report button.
Due to reuseable cells, the same likeButton will be used on multiple cells depending on if it's shown or not. If you change the color of one instance it will keep that color when it is reused again for another cell. Instead of setting the color in the click method you should determine if the button should be red or not in the cellForRowAtIndexPath method. So something like:
var likedRows: Set<Int> = Set()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
self.markButtonIfLiked(cell.button, atRow: indexPath.row)
...
}
#IBAction func likeButtonTapped(button: UIButton) {
...
self.likedRows.insert(button.tag)
self.markButtonIfLiked(button, atRow: button.tag)
...
}
func markButtonIfLiked(button: UIButton, atRow row: Int) {
if (self.likedRows.contains(row)) {
button.setTitleColor(.redColor(), forState: .Normal)
}
}
And you shouldn't need the tableView.reloadData() call.

Resources