Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a few techniques you can use that will help clean up your code and allow for a less messy solution. I will take a wack at explaining this.</p> <p>One first easy step is to create a property or method that gives you the array of data based on the tableView or tag:</p> <pre><code>- (NSArray *)dataForInteger:(NSUInteger)value { return @[self.matchedNames, self.feedArray][value]; } // you can also make this a readonly property for easier access </code></pre> <p>In order to use the above method you need to make sure the tag for the table that needs <code>matchedNames</code> is <code>0</code> and <code>feedArray</code> is <code>1</code> (or you could use a simple <code>if/else</code> statement and make the tags whatever you want.</p> <p>Now look how easy this makes your future logic!</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { [[self dataForInteger:tableView.tag] count]; } </code></pre> <p>That's it!</p> <p>Next, you could potentially create a method like:</p> <pre><code>- (UITableViewCell *)emptyCellForTableView:(UITableView *)tableView { NSString *cellIdentifier = @[@"CellIdentifierONe", @"CellIdentifierTWO"][tableView.tag]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { // the only messy looking logic if (!tableView.tag) { // tableView.tag == 0 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; CGAffineTransform transform = CGAffineTransformMakeRotation(3.14); cell.transform = transform; } else { // make sure "CommentsCustomCell" is a class and not an instance (like your example) cell = [[CommentsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } } return cell; } </code></pre> <p>Then in your <code>tableView:cellForRowAtIndexPath:</code>:</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self emptyCellForTableView:tableView]; NSArray *dataArray = [self dataForInteger:tableView.tag]; // setup all custom UITableViewCell's to have a property or setter "data" if ([cell respondsToSelector:@selector(setData:)]) { // I will explain this a bit later cell.data = dataArray[indexPath.row]; } else { // based on your example this was an NSString array self.textLabel.text = dataArray[indexPath.row]; } } </code></pre> <p>Now the <code>cell.data = dataArray[indexPath.row];</code> is per Apple's example how setup data for custom <code>UITableViewCells</code>. Just create a property called data, that takes in it's setter looks something like this (for your example):</p> <pre><code>// CommentsCustomCell.m - (void)setData:(id /*or custom class, in this case you use NSDictionary*/)data { _data = data; // NTOE: I just copied your example above, I am sure there is a nicer way to do this but you get the idea. NSDictionary *feedPost = (NSDictionary *)data; NSString *checkIFempty = [feedPost objectForKey:@"isForum_empty"]; if (![checkIFempty isEqualToString:@"1"]) { cell.noCommentsLabel.text = nil; } else { cell.noCommentsLabel.text = @"No Comments"; cell.forumComment = nil; } NSString *username =[feedPost objectForKey:@"Username"]; NSString *final_time =[feedPost objectForKey:@"final_time"]; NSString *userIDforPic =[feedPost objectForKey:@"UserID"]; finalComment = [feedPost objectForKey:@"comment"]; cell.forumComment.text = finalComment; } </code></pre> <p>The reason why a solution like this is so beautiful is if you ever wanted to use a different custom <code>UITableViewCell</code> all you would need to do is change the Empty Cell Creation method to create that class, and that is basically it (This also allows you to make all of your cell's <code>IBOutlet</code>s private!). As long as the new class has a <code>data</code> property your main controller does not care! It just sends the data through. This also allows you to scale it appropriately. If all the <code>tableView</code>'s used custom cells you could easily have a dozen tableviews in the same <code>viewController</code> without needing much extra logic (just add it the array, set the tag, and make sure the cell creation method creates the appropriate cell!).</p> <p>I typed this up pretty quickly so there is probably some missing logic or typos. I will fix any if they are pointed out. Good luck.</p>
 

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