Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>i did the following:</p> <p>AutoSectionTableViewDataSource.h</p> <pre><code>@protocol AutoSectionTableViewDataSource - (NSString*)tableView:(UITableView*)tableView sectionNameAtIndexPath:(NSIndexPath*)indexPath; @end @interface AutoSectionTableViewDataSource : NSObject &lt;UITableViewDataSource&gt; - (id)initWithDataSource:(NSObject&lt;UITableViewDataSource,AutoSectionTableViewDataSource&gt;*)dataSource; @end </code></pre> <p>AutoSectionTableViewDataSource.m</p> <pre><code>@interface AutoSectionTableViewDataSource () @property(weak) NSObject&lt;UITableViewDataSource,AutoSectionTableViewDataSource&gt; *dataSource; @end @implementation AutoSectionTableViewDataSource - (id)initWithDataSource:(NSObject&lt;UITableViewDataSource,AutoSectionTableViewDataSource&gt;*)dataSource { self = [super init]; self.dataSource = dataSource; return self; } - (BOOL)respondsToSelector:(SEL)selector { if ([super respondsToSelector:selector]) return YES; if (self.dataSource &amp;&amp; [self.dataSource respondsToSelector:selector]) return YES; return NO; } - (void)forwardInvocation:(NSInvocation*)invocation { if (self.dataSource &amp;&amp; [self.dataSource respondsToSelector:invocation.selector]) { [invocation invokeWithTarget:self.dataSource]; } else { [self doesNotRecognizeSelector:invocation.selector]; } } - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector { if (self.dataSource) return [self.dataSource methodSignatureForSelector:selector]; return nil; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { NSInteger rows = [self.dataSource tableView:tableView numberOfRowsInSection:0]; NSMutableSet *set = [[NSMutableSet alloc] init]; for (NSInteger row=0; row&lt;rows; row++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0]; NSString* sectionName = [self.dataSource tableView:tableView sectionNameAtIndexPath:indexPath]; [set addObject:sectionName]; } return set.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger rows = [self.dataSource tableView:tableView numberOfRowsInSection:0]; NSMutableDictionary *tmp = [[NSMutableDictionary alloc] init]; NSInteger count = 0; for (NSInteger row=0; row&lt;rows; row++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0]; NSString* sectionName = [self.dataSource tableView:tableView sectionNameAtIndexPath:indexPath]; if (!tmp[sectionName]) { tmp[sectionName] = @(tmp.count); } if ([tmp[sectionName] intValue] == section) { count++; } } return count; } - (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSInteger rows = [self.dataSource tableView:tableView numberOfRowsInSection:0]; NSMutableSet *set = [[NSMutableSet alloc] init]; for (NSInteger row=0; row&lt;rows; row++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0]; NSString* sectionName = [self.dataSource tableView:tableView sectionNameAtIndexPath:indexPath]; if (![set containsObject:sectionName]) { [set addObject:sectionName]; if (set.count - 1 == section) { //NSLog(@"AutoSectionTableViewDataSource titleForHeaderInSection:%d -&gt; %@", section, sectionName); return sectionName; } } } return nil; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger rows = [self.dataSource tableView:tableView numberOfRowsInSection:0]; NSMutableDictionary *tmp = [[NSMutableDictionary alloc] init]; NSInteger count = 0; for (NSInteger row=0; row&lt;rows; row++) { NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:row inSection:0]; NSString* sectionName = [self.dataSource tableView:tableView sectionNameAtIndexPath:rowIndexPath]; if (!tmp[sectionName]) { tmp[sectionName] = @(tmp.count); } if ([tmp[sectionName] intValue] == indexPath.section) { count++; if (count-1 == indexPath.row) { UITableViewCell *cell = [self.dataSource tableView:tableView cellForRowAtIndexPath:rowIndexPath]; return cell; } } } return nil; } @end </code></pre> <p>and then in your code:</p> <pre><code>- (NSString*)tableView:(UITableView *)tableView sectionNameAtIndexPath:(NSIndexPath *)indexPath { return @"this would be your section name for indexPath.row" } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return total_number_of_rows; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return cell_for_indexPath.row; } - (void)viewDidLoad { [super viewDidLoad]; // instead of self.tableView.dataSource = self; use: self.autoSectionDataSource = [[AutoSectionTableViewDataSource alloc] initWithDataSource:self]; self.tableView.dataSource = self.autoSectionDataSource; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload