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 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
Hello, I am new in iOS development. What is the best way of learning iOS and Programming also?
You could start learning objective C for iOS app development. Please contact us for more information.
Vikas
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
How much fees for IOS developer training and its exact training time?
it's not about time, it's all about how quickly we can grasp all the iOS development techniques I teach u, I mean course could be for 2 r 3 months, 1 hr/day . some students even take 4 months to learn...
Shubham

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...

On the Job training is always best
On the job training always provides an opportunity to learn the best industry practices. While you work on real time you would encounter many challenges that will force you to learn many new things. Class...

MapKit Tutorial with Swift in iOS 8
Using Map Kit, the portion of the map that is displayed on the screen is referred to as the region. The region is defined by a center location and a span of the surrounding area to be displayed. Inside...

Phone number validation in swift language
Simple phone number validation in swift ios 8 , Using this function you can easily check phone number . Also have some example for this using NSPredicate etc . you can see lots of method for number...

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...

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