63575

Populating multiple sections tableView using pickerView

Question:

I have a tableview whose data are selected from a pickerView(3 columns).Here is the code:

- (void)viewDidLoad { [super viewDidLoad]; //PickerView content and sort them for displaying Number=@[@"Trans",@"2005",@"2006",@"2007",@"2008",@"2009",@"2010",@"2011",@"2012",@"2013",@"2014",@"2015",@"2016"]; Season=@[@"Spring",@"Summer",@"Fall"]; Course=@[@"CHEM1100 General Chem I",@"CHEM2100 General Chem II",@"CHEM3511 Org Chem",@"CHEM3521 Org Chem II"]; Course =[Course sortedArrayUsingSelector:@selector(compare:)]; Number =[Number sortedArrayUsingSelector:@selector(compare:)]; Season =[Season sortedArrayUsingSelector:@selector(compare:)]; _tableView.delegate = self; _tableView.dataSource = self; }

To populate the tableView, I use a button, when the button is pushed, the selection of pickerView will send data to tableView

- (IBAction)addCourse:(UIButton *)sender { NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent]; NSString *num=Number[numRow]; NSString *season=Season[SeaRow]; NSString *course=Course[CourseRow]; NSString *CourseToAdd=[[NSString alloc ]initWithFormat:@"%@ ",course]; NSString *SeasonToAdd=[[NSString alloc ]initWithFormat:@"%@ ",season]; NSString *YearToAdd=[[NSString alloc ]initWithFormat:@"%@ ",num]; [self.msgCourse addObject:CourseToAdd]; [self.msgSeason addObject:SeasonToAdd]; [self.msgYear addObject:YearToAdd]; [_tableView reloadData]; }

To show the data in tableView, I use

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = nil; cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if(!cell){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; } cell.textLabel.text=[self.msgCourse objectAtIndex:indexPath.row]; return cell; }

To determine the rows and sections

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ //return [self.msgYear count]; return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.msgCourse count]; }

It is running fine. The output is shown in the picture attached. <strong>Here are my questions:</strong>

(1) I want the tableView shown in more than one sections. For example, "2005 Fall" is one section, inside this section, the courses are shown. In other words, the first two columns selected in the pickerview will form the section title.

(2) How to sort the results in each section?

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/1meKr.png" data-original="https://i.stack.imgur.com/1meKr.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer1:

You could define number of sections and rows in it as following:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // number of courses? return 3; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return ((section == 0) ? 1 : 3); } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // return appropriate cell(s) based on section if(indexPath.section == 0) { // Return 1 cell } else if(indexPath.section == 1) { switch(indexPath.row) { case 0: // Initialize cell 1 break; case 1: // Initialize cell 2 break; ... } } return cell; }

Here are the good tutorials to learn more:

<ul><li><a href="http://www.appcoda.com/ios-programming-index-list-uitableview/" rel="nofollow">http://www.appcoda.com/ios-programming-index-list-uitableview/</a></li> <li><a href="http://briterideas.blogspot.com/2012/07/uitableview-how-to-part-3-multiple.html" rel="nofollow">http://briterideas.blogspot.com/2012/07/uitableview-how-to-part-3-multiple.html</a></li> <li><a href="https://owenandrewsblog.wordpress.com/2012/01/29/uitableview-sections-tutorial/" rel="nofollow">https://owenandrewsblog.wordpress.com/2012/01/29/uitableview-sections-tutorial/</a></li> </ul>

Recommend

  • Paste yanked block with `p` gets mixed with lines below
  • How to read a numeric value (i.e. double value) from excel sheet using apache poi
  • How to enable httpsrc plug-in in my gstreamer?
  • Xpath Regex in PHP not working
  • do I have to release the NSDate in this code below?
  • Direction of target annotation when outside of visible area
  • Badge value on “More” tab
  • Removing per-panel unused factors in a bar chart
  • access an alertView's calling view
  • UIdatepicker showing weekdays
  • Installing Flash Player on an Android Emulator
  • Using JQuery Datepicker To Only Show Months
  • Convert row values into columns in R
  • How to link a work load item with a commited change?
  • Is Lambda code repeated with dynamically created controls?
  • How would you model data variables variance on common scheme? SQL
  • Pandas plotting two graphs on one scale
  • Fluent Nhibernate: Trying to create entity with composite key that is also the keys for two referenc
  • Need advice in designing tables in SQL-Server
  • Customize google placepicker colors for android
  • JFreeChart BarChart - Category Markers
  • Regex: Match everything except backreference
  • How to not let setText crash/freeze my whole application? Even using SwingUtilities and Threads?
  • PostgreSQL 9.1 timezones
  • A simple datepicker in VueJS
  • ResponseBuilder is not working when used with entity object
  • Error Code: 1054. Unknown column
  • UIPickerView without IB?
  • Escaping single quotes in JDBC with MySql
  • How to access recipient on sent messages page with mailboxer
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • Click on button in another program - FindWindow, C#
  • How to make R's read_csv2() recognise the text characters properly
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • c# open webrowser in many tab
  • C: Incompatible pointer type initializing
  • how to save the state in userdefaults of accessory checkmark-iphone
  • How to Cache Real-time Data?
  • AT Commands to Send SMS not working in Windows 8.1
  • Compare two NSDates in iPhone