How do I call a specific TableViewCell - ios

The first cell in this TableViewController is suppose to alert the user when their status is liked or favorited. I need to create a function so when a button is tapped, it will call this table view cell and notify them who liked their photo. I am just not sure how to call this specific table view cell. Can anybody help me out? I have searched SO and Apples Swift Documentation with no luck.

In your didSelectRowAtIndexPath tableview delegate method you can get the index path (row position) of the cell they tapped on and then if you've got an array of data (used to populate the tableview in the first place) you know where to look to find out which notification/activity it is:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedItem = items.objectAtIndex(indexPath.row) as String
}
You can also get the cell by adding this inside the method:
let cell = tableView.cellForRowAtIndexPath(indexPath)

You can get the reference to the cell with cellForRowAtIndexPath method of UITableView.

Related

How to run a function when tableviewcell is selected (for example, update UIImageView when cell is pressed)

I'm making an app that has a collection table view that when a cell is selected it segues into another view controller with a tableview.
I want to use this tableview to update a UIImageView in my VC. So let say i press "Situps" on the TBC i want the imageview to update to an animation that shows situps. Hope someone can understand what im trying to do, thanks for the help!
The delegate method tableView(_:didSelectRowAt:) is called when a cell is tapped on.
You can insert your logic to either segue to another viewController or update an imageView in the same viewController in this method.
for Swift 3.0:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//your code...
}
Documentation: https://developer.apple.com/reference/uikit/uitableviewdelegate/1614877-tableview
Your problem is quite simple.
use method.
tableView(_:didSelectRowAt:)
then put your method here inside did select and you can navigate to viewcontroller you want to.
You can try this:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as UITableViewCell
//so you can do whatever you want on your cell
}
Yes, you can trigger any action of a cell using the didSelect delegate method of Tableview but...
I think that what you are trying to do is to make an update in one VC from other? is that the case, what you need is to use delegation or NSNotificationCenter to send updates between classes.

How to disable one specific tableView cell in ios Swift

I have created a custom UITableViewCell.I have only two rows in my tableView. Now I want to disable 2nd cell when 1st is tapped and disable 1st cell when 2nd is tapped.
How can I do that ? please Help and guide
Implement the UITableViewDelegate method willSelectRowAtIndexPath.
In that method, return the indexPath value that's passed to you if you want the user to be able to select it, or return nil if you don't want it to be selectable.
I'll leave it to you to figure out the logic that decides when different cells should/should not be selectable.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(NSIndexPath(forRow: (indexPath.row + 1) % 2, inSection: 0))?.userInteractionEnabled = false
}

Find Out Which Section Button Belongs To In UITableView When Clicked Inside Cell

I am trying to change the value of an element of an array depending which section a button is clicked in. For example say I have this array numbers = [0,0,0,0,0] and I want to change the first element to 5. I insert 5 into the cell of the first section and click done in that same section and the array will now read [5,0,0,0,0]. Is there a way to know which section the button belongs to?
Right now I have two separate classes. One for the custom cell and one for the tableview. Both of the have an outlet to the button. When the button is clicked the custom cell class changes a temporary global number to the inserted number. And inside the table class I want the button action to take that global number and insert in into the element that is the same number as the section the button belongs to. Except I don't know how to find out which section it belongs to.
Can anyone help me out with this? I'm writing in Swift btw.
If you only need the section (not the section and row) of the index path of the cell containing the tapped button, you could tag the button with the section number (Int) when you configure the table view cell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier", forIndexPath:indexPath) as! MyCustomTableViewCell
cell.myCustomButton.tag = indexPath.section
// Remove all existing targets (in case cell is being recycled)
cell.myCustomButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
// Add target
cell.myCustomButton.addTarget(self, action:"buttonAction:", forControlEvents:.TouchUpInside)
return cell
}
func buttonAction(sender:AnyObject)
{
if let button = sender as UIButton{
println("Tapped Button in section: \(button.tag)")
}
}
If you need both the section AND the row, you're better off storing the whole index path. For that, you can either use a custom UIButton subclass with a property of type NSIndexPath, or store it in the table view cell.
But the second solution is a bit messy since you have to access the table cell (and then, the index path) from the button by using superview, etc., and this relies on the table cell's subview structure. I'm not sure if the parent view of your button is the table cell itself, or its "content view" (I'm a bit rusty right now...)
If you want to find which cell has been 'clicked' you could use the UITableViewDelegate method.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}

Can't get cell from previous row in cellForRowAtIndexPath

I have a tableView with a bunch of custom tableViewCells. I have a button that lets the user add a new cell at the bottom of the tableview. I want this new cell to have some data displayed depending on the data displayed in the cell above it. So, in my tableView(cellForRowAtIndexPath) delegate method, I call tableView.cellForRowAtIndexPath for the previous row and cast the result to my custom cell. However, this causes a crash.
if (indexPath.row > 0) {
let previousCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: indexPath.row - 1, inSection: indexPath.section)) as! myCustomCell
}
Interestingly, if I don't force the cast, it doesn't crash. However, this is not useful for me since I need to force the cast and access some properties that only exist in my custom class. I am sure that the cell at (indexPath.row - 1) is a cell of my custom class.
As mentioned in the comments, UITableView.cellForRowAtIndexPath(_ indexPath: NSIndexPath) returns nil when the index path points to a cell that is currently not on the screen. See also the official documentation.
Instead of calling the method on the table view, you could call it on the table view's data source. UITableViewDataSource.tableView(_ tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) is a function you have to implement and it should return a proper cell regardless of whether the index path is on- or off-screen.
However, an even better approach (again, as mentioned in the comments) would be to get the cell contents from your data model instead of the cell.

Handling button interaction of a dequeueReusableCell in iOS Swift

I am making an iOS app that relies on a table view. In each cell of the table view, there are 4 buttons aligned on the bottom. I have a cell class that is pretty standard and a feedController to handle the table and setting all the items of the cell.
Everything works fine on it but I can not figure out how to handle the button clicks within the cell. I can hard code it into my cell class, but then every 3 cells has the same interaction. Is there a way to pass the button click function from the cell class into the controller? I have tried checking the state from the controller and that has not worked.
Can you add a gesture recognizer as you're doing your cellForItemAtIndexPath? So I had something similar with a collection view, and what I did was as it within:
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as MyCollectionView
...
I would add a gesture recognizer to each cell
i.e.
cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action:Selector("tapAction:")))
And then something like:
func tapAction(recognizer: UITapGestureRecognizer) {
...
}
so recognizer ends up being the specific item tapped, and I could take action accordingly (in my case, I had my datasource of items and I would find the item in an array by casting recognizer to a cell, finding the appropriate subview, and update values on it)
I would add code block properties to your cell class which the table can assign to deal with each button. In your cell, code each button handler to call the appropriate block, or pass an index for the button used in a single block.
See my answer here which has an example, but for a switch.
How can I get index path of cell on switch change event in section based table view
If after a few cells you get the same interaction, it's possibly because you're dequeueing a reusable cell, and you're getting the same cell.
Make sure to set your .setTarget() call for your buttons in your tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) data source every time the cell is dequeued. It would help if you shared how you're handling dequeuing to see if this is your issue.

Resources