Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:30:02+00:00 2026-06-18T14:30:02+00:00

While looping the – (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {} method im setting data to

  • 0

While looping the

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

method im setting data to my custom cell (an image and a text). I was wondering what’s the best way to achieve this.

I can use 2 different array (image array and text array) and do something like:

cell.image setImage: //get image from imageArray with indexPath.row
cell.text.text = [textArray objectAtIndex:indexPath.row];

or use a multi dimensional array like this:

cell.image setImage: [imageArray objectAtIndex:indexPath.row] objectAtIndex:0;
cell.text.text = [textArray objectAtIndex:indexPath.row] objectAtIndex:1;

// or use of dictionary with keys

What method is quicker or more readable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:30:03+00:00Added an answer on June 18, 2026 at 2:30 pm

    Personally I think the following is the cleanest solution:

    • Create a model for the items in your array.
    • Create an UITableViewCell subclass to display the model in the cell. The subclass will have a property that accepts the model and redraw itself when the model changes.

    Let’s say we have a news app. The array is filled with items for which we create the model NewsItem. The model header could look like this:

    NewsItem.h

    @interface FSNewsItem : NSObject <NSCoding>
    
    @property (nonatomic, copy, readonly)   NSString *title;
    @property (nonatomic, copy, readonly)   NSURL    *URL;
    @property (nonatomic, copy, readonly)   NSString *time;
    @property (nonatomic, copy, readonly)   NSString *points;
    @property (nonatomic, copy, readonly)   NSString *author;
    
    // initialiser
    + (FSNewsItem *)newsItemWithTitleNode:(HTMLNode *)node 
                              subTextNode:(HTMLNode *)subNode;
    
    @end
    

    Now for the cell we create a NewsItemCell. The code for NewsItemCell might look like the following:

    NewsItemCell.h

    @interface FSNewsItemCell : UITableViewCell
    
    @property (nonatomic, strong) FSNewsItem *newsItem;
    
    @end
    

    NewsItemCell.m

    @interface FSNewsCell ()
    
    @property (nonatomic, strong) UILabel *titleLabel;
    @property (nonatomic, strong) UILabel *detailLabel;
    
    @end
    
    
    @implementation FSNewsItemCell
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self)
        {
            self.titleLabel                       = [[UILabel alloc] init];
            [self addSubview:_titleLabel];
    
            self.detailLabel                      = [[UILabel alloc] init];
            [self addSubview:_detailLabel];
        }
        return self;
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        const CGRect bounds = self.bounds;
        CGFloat width = bounds.size.width - (FS_FLOAT_PADDING * 2) - 15.0f;
        _titleLabel.frame = CGRectMake(FS_FLOAT_PADDING, FS_FLOAT_CELL_PADDING_VERTICAL, width, 20.0f);
    
        CGFloat y = _titleLabel.frame.size.height + (FS_FLOAT_CELL_PADDING_VERTICAL * 2);
        _detailLabel.frame = CGRectMake(FS_FLOAT_PADDING, y, width, 15.0f);
    }
    
    #pragma mark - Private
    
    - (void)setNewsItem:(FSNewsItem *)newsItem
    {
        if (_newsItem == newsItem)
        {
            return;
        }
    
        _newsItem = newsItem;
    
        self.titleLabel.text = newsItem.title;
    
        if (_newsItem.points && _newsItem.time)
        {
            self.detailLabel.text = [NSString stringWithFormat:@"%@ | %@", _newsItem.points, newsItem.time];
        }
        else
        {
            self.detailLabel.text = newsItem.time;
        }
    
        [self setNeedsLayout];
    }
    

    Finally, when we want to display the news item in the cell, our code would look like this:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        FSNewsItemCell *cell = (FSNewsItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[FSNewsItemCell alloc] initWithStyle:UITableViewCellStylePlain reuseIdentifier:CellIdentifier];
        }
    
        cell.newsItem = [_items objectAtIndex:indexPath.row];
    
        return cell;
    }
    

    This is in my opinion the cleanest solution and the approach I take most of the time. I like to keep my view controllers small. I also like the fact that my view controller doesn’t have to know which controls my (custom) table view cell has. The table view cell gets full responsibility on how to draw itself depending on the data supplied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While looping through a Dataset which code snippet should I use, should I go
I am having a problem while looping through one of my loop constructions. I
I am working on appending a file extension while looping over an array. The
I have a function that is normally called while looping inside a SqlDataReader, that
I'm familiar with the problem of modifying a collection while looping over it with
How can I get the next record from a collection while looping through a
I need your help. I have tables using PHP while looping and I need
I get the following error while I try to delete a row while looping
While looping through records in one list, I want to find a corresponding line
While looping through all objects I want to render in my 3D-engine, I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.