Change title of UITableView to a cell name in iOS - ios

I have a UITableView with some rows that are allocated dynamically. When I tap on a row I will go to another TableView using a push Segue, how do I change the second table view title to the cell name of the first table view cell that was tapped?

Just use the following code inside tableViewDidSelectRowAtIndexPath method
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
NSString *title=cell.textLabel.text;
Now you can pass the string title to the next table view.

I made a string where i stored the cell title in tabeViewDidSelectRowAtIndexPath and I passed it in the prepareForSegue but the title string but when I tapped on the cell the first method to be called was prepareForSegue and the string would get instantiated after that so i called
[self performSegueWithIdentifier: #"nameOfSegue" sender: self];
in tabeViewDidSelectRowAtIndexPath and it worked.

In swift try
let selectedCell = self.SomeTableView.cellForRow(at: indexPath) as? SomeTableViewCell
if let cell = selectedCell {
// get text from cell label for example
}

Related

Display Two different UITABLEVIEWCELLS outside of cellForRowAtIndexPath

I have two UITableViewCells and displaying them based on condition in
cellForRowatIndexPath. Both cells related to a creation of a post and than displaying them in UITableView.
In cellForRowAtIndexPath method i don't have any issues to use condition to display cell.
For example:
if postType[indexPath.row] == "Regular" {show this sell }
else {show another}`
This is working perfect. postType array is created during the post creation.
The issue that i have is to show a proper cell outside of cellForRowAtIndexPath method.
I have a button and when user click on it they would be going to the proper cell, just like they would click on the cell it self. However I don't know how to condition an array at proper indexPath or maybe there other way. I'm not that good because just starting out to learn swift.
I can do for a one cell but i want a condition first and than display a proper cell.
This statement works for a one cell.
#IBAction func usernameBtn_click(_ sender: AnyObject) {
let i = sender.layer.value(forKey: "index") as! IndexPath
let cell = tableView.cellForRow(at: i) as! postCell
}
In my case I have two cells. How to have a condition based on post type value in array and have a proper index inserted to check the value and than display a cell.
I have these two cells:
let cell = tableView.cellForRow(at: i) as! postCell
let cell = tableView.cellForRow(at: i) as! moreinfoCell
Any suggestion would be helpful.
You mention that the button does the same as if the user selects the cell so if you have that working just deselect "User Interaction Enable" of your button.
In interface builder:
Programmatically:
button.isUserInteractionEnabled = false
Doing that the touch will be done on the UITableViewCell and the didSelectRowAt indexPath: method will be fired.

how to get current cell.contentview in uicollectionview. how to add subview in uicollectionview cell

Hi in my application i want to add particular view in cell.contentview now situation is that where i can add this code. reason is if i am puting this code on cellforitematindexpath it will be added in last cell(indxpath.row) reason is in cellforrowatindexpath last cell will be loaded last so view will be added in last cell. while i want it on that cell which was displaying at current time
Here is my code. simply have to add subview
let cell : pdfImageCell = collectionView.dequeueReusableCell(withReuseIdentifier: "pdfImageCell", for: indexPath) as! pdfImageCell
cell.contentView.addSubview(userResizableView1)
Get the visible cell index like below in UICollectionView. You can also get multiple cells visible if they are visible on screen so selected the cell you want.
var visibleCurrentCell: IndexPath? {
for cell in self.collectionView.visibleCells {
let indexPath = self.collectionView.indexPath(for: cell)
return indexPath
}
return nil
}
Then get the cell from your index and add the subview in that cell.
You can add UIView in awakeFromNib() method. It will get the call only once.
class pdfImageCell : UICollectionViewCell {
//you can add here
override func awakeFromNib() {
var userResizableView1 = UIView()
self.contentView.addSubview(userResizableView1)
}
}
- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths
Here it is a method to reload the specific indexPaths in your collectionView
you can update your datasource on click and your cellForItemAtIndex will know from your data source that it has to have the subview this time
if (datasourcearray[indexpPath.row].isSubViewToBeAdded)
{
self.contentview.addsubview(subViewToBeAdded)
}

How to pass Addon items on buttonclick in customcell in swift?

I have a tableview with variable number of cells representing addon items. They are custom cells with a checkbox button which triggered to new ViewController.
How can I pass the Addon items which is displayed in cell on triggered the button?
I have tried to pass the cell number using tag property from the button but what about the data passing on button click.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("itemCell")as! ItemTableViewCell
cell.itemName.text = (items?[indexPath.row] as? [String:AnyObject])?["name"] as? String
cell.itemPrice!.text = "£\((items?[indexPath.row] as? [String:AnyObject])?["price"] as! String)"
itemAddon = (items![indexPath.row] as! [String:AnyObject])["addon"] as? String
cell.orderBtnOutlet.tag = indexPath.row
return cell
}
I have to pass itemname, itemPrice, and itemaddon to ItemTableViewCell based on its button click on each cell. How can I pass??
If you have access to button in your presented vc, you can access cell by let cell = button.superview as! ItemTableViewCell
Then you can access cell labels text.
But it is not good approach.
If you assigned button action in storyboard to present anothet virw controller, it anyway has segue. Assign it an identifier and in your tableviewcontroller override method prepareForSegue, where you can assign needed properies of targetVC

Access Views inside custom UITableViewCell in iOS

I have a UITableView which is populated by the cells coming from another XIB file. How can I access the views (eg. label) of the cell inside didSelectRowAtIndexPath method of tableView?
Use cellForRowAtIndexPath:
let cell = tableView.cellForRowAtIndexPath(indexPath) as! YourCustomCell
then you can access its label:
cell.customLabel.text = "test"

Detect UIButton while using custom UITableViewCell

I want to implement a custom UITableViewCell class that is made up of subviews such as, labels, buttons, and images. These cells will display content fetched from the web using an API.
I do not want to implement the UITableView delegate method didSelectRowAtIndexPath as this will make the entire cell selectable. Only the button in the cell should be able to trigger any action.
The button is connected from the storyboard to the custom UITableViewCell via IBOutlet. The addTarget(_:action:forControlEvents:) method is called on the UIButton in cellForRowAtIndexPath of the UITableViewController class.
The roadblock occurs when we want to detect the indexPath of the selected cell in the Selector function of the button.
This is how the indexPath for the selected cell can be detected in the Selector function
#IBAction func doSomething(sender: AnyObject) {
var location: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(location)!
println("The indexPath for Selected Cell is - \(indexPath.row)")
}
Although, this successfully gets me around the issue, my question is;
1) Have you found an alternate way of being able to use UIButtons to pass selected cell data in a custom UITableViewCell?
2) What would be the best practice, so far, in Swift to implement a similar scenario?
One way would be to iterate over the superviews of the sender and see if a UITableViewCell can be found...
Let's say you created a generic UIView extension with a method that would check to see if any of its superview's was a UITableViewCell...
extension UIView {
func parentTableViewCell() -> UITableViewCell? {
var view = self
while let superview = view.superview {
if let cell = superview as? UITableViewCell {
return cell
} else {
view = superview
}
}
return nil
}
}
Then you could do the following...
if let cell = (sender as UIView).parentTableViewCell() {
let indexPath = tableView.indexPathForCell(cell)
println("The row for this cell is - \(indexPath.row)")
}
Another way would be to use the tag property of a view by setting it to an Int and then check to see what the tag of the sender was in the method.
i.e. In your tableView:cellForRowAtIndexPath: method
cell.myButton.tag = indexPath.row
Then in your doSomething method
let rowIndex = (sender as UIButton).tag
println("The row for this cell is - \(rowIndex)"
If your tableView only has one section, you can use the tag property of the button to store the indexPath.row.
In cellForRowAtIndexPath, when you set the target-action for the button, set button.tag = indexPath.row.
Then in your doSomething routine:
#IBAction doSomething(sender: UIButton) {
println("This button is from row - \(sender.tag)")
}

Resources