How do you make a UI TableView have different numbers of rows in different sections?

Asked by Last Modified  

Follow 4
Answer

Please enter your answer

3 down vote accepted You can't use loop like you're doing since that just gives each cell in that section the value in the last index that you loop through (index 2, or 4). Normally, for a sectioned table view, you would use an array of arrays to populate the sections. If you want to use a single...
read more
3 down vote accepted You can't use loop like you're doing since that just gives each cell in that section the value in the last index that you loop through (index 2, or 4). Normally, for a sectioned table view, you would use an array of arrays to populate the sections. If you want to use a single array, and the number of rows in each section stay like you show them, then the following should work, Example:- class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { var titles=["1", "2", "3", "4", "5"] override func viewDidLoad() { super.viewDidLoad() } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 3 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section== 0 { return 3 } else if section== 1 { return 1 } else { return 2 } } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let myCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell if indexPath.section == 0 { myCell.textLabel!.text = titles[indexPath.row] } else if indexPath.section == 1 { myCell.textLabel!.text = "Section 2" } else { myCell.textLabel!.text = titles[indexPath.row + 3] } return myCell } } read less
Comments

Tutor

We have Delegate methods numberOFRowsForSection:(NsInteger )Section In This method section is the parameter to which you want to return row count.. suppose for first section (that means as our index start with 0 section value will be 0) you can return 2 rows... In the Above mentioned method you can...
read more
We have Delegate methods numberOFRowsForSection:(NsInteger )Section In This method section is the parameter to which you want to return row count.. suppose for first section (that means as our index start with 0 section value will be 0) you can return 2 rows... In the Above mentioned method you can right a if condition that is section is 0 need to return 3 rows If (section == 0) return 3l read less
Comments

Technical Architect (14+ years in IT, 10+ years in iOS, Polyglot programmer, C/C++,Python,Golang)

check this method: https://developer.apple.com/documentation/uikit/uitableview/ method: Swift numberOfRows(inSection section: Int) -> Int Objective C - (NSInteger)numberOfRowsInSection:(NSInteger)section; Here you can decide based on which section how many rows should be returned.
Comments

Trainer

We can achieve this by implementing the UITableviewDataSource delegate method 'numberofRowsInSection', which gives us a section parameter where we can return the required number of rows for each section. PFB some sample code - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section...
read more
We can achieve this by implementing the UITableviewDataSource delegate method 'numberofRowsInSection', which gives us a section parameter where we can return the required number of rows for each section. PFB some sample code - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch (section) { case 0: return 5; case 1: return 4; default: return 1; } } read less
Comments

View 2 more Answers

Related Questions

What are the best sources to learn iOS mobile application development?
Search the best IOS developer and learn from that guys or you could find the best institute or trainer who gave the depth knowledge for develop the IOS Apps
Pratibha
What courses can I do to become an iOS app developer specializing in games?
Hi Rajeev, You can take two approaches : 1) Learn the native iOS Game Development Framework. It is called SpriteKit. For 3D games, you have SceneKit and Metal. If you already have iOS App development...
Rajeev J
0 0
6
What is the fees structure of IOS training and what is the time duration?
you will get different answers from different ppl and institutes....i would invite you to a conversation where we can discuss your needs to adapt the duration and thus the fees as well.
Shivangi
0 0
8

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Working With Arrays in Swift
Swift Arrays:Like Objective C arrays Swift arrays are also collection of Objects The Difference is in swift we don't work directly with addresses.The declaration of Swift array is as follows The above...

Learning Path For Mobile Application Development
I. Traditional Approach: 1. Native Android Application Development: Android is a mobile operating system that runs on phones, tablets, smartwatches, TVs and more. You can learn Android Application Development...

Developing iOS Apps Part 1
The Basics Swift opts to use a Javascript-like ‘var’ keyword to define any new variable. So for example to create a new variable with a bit of text in it, you would have this: var...

P Balasubramanyam

0 0
0

10 Ways My Life Changed When I Learned To Code
1. I have freedom to make my own schedule. 2. I earn more, but work fewer hours. 3. I can say “no” to job offers. 4. People come to me asking if I can work for them. 5. I never have to...

Email Address Validation in iOS 7
-(BOOL) Emailvalidate:(NSString *)tempMail{BOOL stricterFilter = YES;NSString *stricterFilterString = @”+@(+\\.)+{2,4}”;NSString *laxString = @”.+@(+\\.)+{2}*”;NSString *emailRegex...

Recommended Articles

Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...

Read full article >

Looking for iOS Developer Training?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you