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"
Related
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)
}
I have created a tableview prototype cell in storyboard and I have added a button to cell and set its tag to indexpath.row. When I scroll my cells the scrolled cell on the top of tableview always set tag to zero instead of correct tag.
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("autoLoadReuseIndentifier", forIndexPath: indexPath)
print("indexpath :\(indexPath.row)")
cell.contentView.viewWithTag(100)?.layer.cornerRadius = 5
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
let tempDict : NSDictionary = savedCardsArray.objectAtIndex(indexPath.row) as! NSDictionary
let bankName = cell.contentView.viewWithTag(102) as! UILabel
deleteButton = cell.contentView.viewWithTag(106) as? UIButton
deleteButton?.tag = indexPath.row
deleteButton?.addTarget(self, action: "deleteCard1:", forControlEvents: UIControlEvents.TouchUpInside)
print("delete button:\(deleteButton)")
// print("indexpath delete tag :\(deleteButton.tag)")
if(self.isSetUpAutoloadSelected){
deleteButton?.hidden = true
}else{
deleteButton?.hidden = false
}
return cell;
}
Whenever I scroll the cells, delete button tag is always set to zero.
If you should go with other way so use follow code and get indexPath.
func deleteCard1(_ sender:deleteCard) {
let buttonPosition:CGPoint = sender.convert(CGPointZero, to:self.tableView)
let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
}
I think you don't need to follow this approach because firstly you set button tag statically in storyboard and again you are change it's tag in cellforrowatindexpath so when you scroll, cell will never find button with tag 106.If you want to follow this approach then you need to create customButton and add Variable of type NSInteger or whatever you want and store indexpath.row into that variable of customButton.
Another Approach is Create CustomTableViewCell Class and create button outlet in this custom Cell class and set indexpath.row into button tag like this
CustomCellClassObject.buttonName.tag = indexPath.row
As Sumit said, it’s better to use a custom cell and create outlet for the buttons and labels, as fetching sub views using tags will be tough to maintain the code in the future.
Additionally, you don’t have to create the variable deleteButton, as I don’t see a valid purpose.
Assign tag to the button and add target in cellForRowAtIndexPath, it should work fine.
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
I have one ui table view controller, that has four custom cells, each one of them has a custom ui table view cell.
now let's say i have added some buttons to the third custom ui table view cell, and i want to when one button of them clicked to access data (such as labels) that are in the second custom ui table view cell.
how can i do that?
this is a one example of the third custom ui table view cell
class thirdCellc: UITableViewCell {
//here i want to access data that are in another custom ui table view cell
}
keep on mind that i have also a custom ui table view controller like this:
class PageTwoViewController: UITableViewController {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let rowNumber = indexPath.row
switch rowNumber {
case RequestData.numberOfPeople.rawValue:
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentefiers.numberOfPeopleCell.rawValue) as! NumberOfPeopleTableViewCell
return cell
case RequestData.dayOfMeal.rawValue :
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentefiers.dayCell.rawValue) as! DayTableViewCell
return cell
case RequestData.timeOfMeal.rawValue:
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentefiers.timeCell.rawValue) as! TimeTableViewCell
return cell
case RequestData.preferences.rawValue:
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentefiers.preferencesCell.rawValue) as! PreferencesTableViewCell
return cell
default:
return UITableViewCell()
}
}
}
you need to create an outlet from the tableView to the custom class, then you can write code as follows
#IBOutlet weak var tableView: UITableView! //outlet you created
let indexPath = NSIndexPath() //Get index Path of cell you want in tableView
let cell = tableView.cellforRowAtIndexPath(indexPath) as! FirstCell //Or whatever the other cell class was.
cell.textLabel.text //Now you have all the dot properties of whatever cell at indexPath you passed in.
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
}