This question already has answers here:
Find the indexPath of a button inside UITableViewCell when button pressed?
(6 answers)
Closed 6 years ago.
I have a table view which has cells in it. In each cell there is a button to take you to a new UITableView. I am trying to figure out how to get the index of the cell I clicked the button in from the new UITableView because depending on what cell index I choose it needs to show different items in the new UITableView it takes you to. Any help would be appreciated.
set tag property of the button equal to the indexPath.row in cellForRowAtIndexPath method or in your custom cell class if you are using one.
Hence, when its clicked, you can get the value of indexPath.row from its tag (sender.tag).
Note: Assuming there is only one section in your tableview and you wont be inserting or deleting rows runtime. You will have to reload the table in such scenarios.
The best solution that I personally use is, to pass the cell a model object via property or custom method. And later, use the object to make further decisions.
Related
This question already has answers here:
How to have one static cell in a dynamic tableview?
(3 answers)
Closed 3 years ago.
I'm trying to make a view similar to the one on the screenshot.
The question is what is the first element(the one with label and chevron)
When I tap on it, it behaves like a table cell(on the screenshot I captured it in tapped state). Thank you!
UPDATE
I ended up creating 2 sections and then checked for index
Looks to me exactly like a standard UITableView cell, with cell.accessoryType = .disclosureIndicator and the background color etc. changed of course.
Honestly looks like the standard table view cell, but you could easily implement this with a custom view (NIB) as well.
You can build this view with UITableViewDelegate like this:
Make a button in the cell and set this image to the button.
Set yourButton.isHidden = true in cellForRowAtIndexPath
Set yourButton.isHidden = false in didSelectRowAtIndextPath
That's it.
This question already has answers here:
detecting uibutton pressed in tableview: Swift Best Practices
(12 answers)
Calling a phone number in swift
(25 answers)
Closed 3 years ago.
I'm trying to develop my first app on IOS platform and i want to add a call button to different cells and give each cell a different number to call because I want to programmatically add number to each cell to call ((for example I want cell A to call only the number 123 when I press the call button and I want the Cell B to only call 456 when I press the call button in that cell , I have a service class where my data is stored and I have DeatilCell class to update my cell with different data and another class to pass my data to the cells
I tried to search for some answers on the internet but I couldn't find anything
You need to do 2 things:
Have the specific phone number based on the index of the cell.
Call it.
There are many approaches to get a value based on events inside UITableView or UICollectionView cells, just pick one.
This question already has an answer here:
UITableview Scroll erases data in text field inside UITableviewcell
(1 answer)
Closed 4 years ago.
I have taken two custom tableview cells in one tableview.One cell is for textfield and another for dropdown.If I scroll the tableview after filling the data in textfields it was removing(data in textfields is removing).Can anyone please help on this.
This is happening bcoz table view cell is reusing(if you dequeued cell, hope you did).
To overcome this, you need to set cell data i.e. textfield data in cellForRowAt:, if user has already entered value init.
You need to track what value is added in which cell textfields so when cell is about to visible cellForRowAt: will be called and you set data in it, so data won't be erased.
This question already has answers here:
Calling setCollectionViewLayout:animated does not reload UICollectionView
(7 answers)
Closed 6 years ago.
I have a UICollectionView that is using grid layout currently I have two cells in a row.
I want to give the user a choice to switch between grid and list layout similar like table view.
How can I proceed for the same.
use segmentcontroller with uicollectionviewcontroller and uitableviewcontroller. then implement delegate methods and datasource methods for both. then hide one view controller tableview or collectionview. then implement the action for segment controller.
when segment index == 0 show collectionview else show tableview. hope this will help to you.
or else you can use same uicollectionveiw with sections.
numberOfItemsInSection shoud return 0 that means there is one item(row) per section. You consider this as list view. you can manage size or look and feel accordingly.
numberOfItemsInSection returns 2 or more whatever number of items you want in your section that means it is grid view.
You have to managa datasource methods accordingly. You should have to set some flag that keep status that user have selected the list or grid.
according to that status you can manage your datasource or delegate.
Second approach :
Use collection view for grid representation and tableview for list representation and just show and hide one of them according to user's choice as a result of grid or list.
I have a uitableview with 2 custom cells (messages, and notifications). I want to apply a segue to another viewcontroller when user press the message cell, but do nothing when he press the notification cell. how to do this with didselectrowatindexpath method? some answers here suggested using indexpath.row for checking which cell is clicked but I get these dynamic cells from json so I don't know where their position is in the tableview or how many are they.
using swift and xcode 7.3
If the cells are different types, you can just determine the class type in didselectrowatindexpath
Or you can add a different tag to the cells. You can do this in the storyboard or programatically when you create the cell.
Then in didselectrowatindexpath check what the tag is for the selected row and you will know what type of cell it is.