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

  • SEARCH
  • Home
  • 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 7492527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:27:26+00:00 2026-05-29T16:27:26+00:00

I’m figuring out the right mechanism to pass data from UITableViewCell s to a

  • 0

I’m figuring out the right mechanism to pass data from UITableViewCells to a UIableViewController (or UIViewController).

Searching within stackoverflow I found different ways to do this and finally I found a mechanism that works well but I don’t know if it could be a valid approach.

This is the scenario. First, I created a custom cell (associated with a xib interface), named DataTableViewCell, that extends UITableViewCell. This cell has some outlet to display (and modify) data and an addictional property called index like the following:

@property (nonatomic, copy) NSIndexPath* index;

This property is refreshed inside the method cellForRowAtIndexPath method within the UITableViewController:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DataTableViewCell *cell = (DataTableViewCell*)[tv dequeueReusableCellWithIdentifier:kCellTableIdentifier];
    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"DataTableViewCell" owner:self options:nil];     
        cell = (DataTableViewCell*)self.dataTableViewCellOutlet;
    }

    // configure the cell with data
    // do stuff here...

    // configure the cell with the current indexPath
    cell.index = indexPath;    

    return cell;
}

Since it is possible to change values within a cell, I had the need to pass data to the UITableViewController for updating the model. To do that I decided to use a delegate mechanism. So, I created a protocol with a method like the following:

- (void)updateData:(DataItem*)dataItem atIndexPath:(NSIndexPath*)index;

The UITableViewController implements that protocol. In this way, within the cell (against to some events), I can call that method and update the model in the correct way.

Having said this, I have some questions to ask:

  1. Is this a right mechanism to pass data from a cell to a controller?
  2. Is it correct to using an index property like the one use in the cell?
  3. Is it the same using retain policy instead of copy policy. What could be the difference?
  4. Since the solution I found could be very scheming, is it possible to use block insteads?

About blocks, I thought this way:

Within the cell create a property block like the following:

@property (nonatomic, copy) void (^updateModelOnEvent)(DataItem* dataItem);

Then inside the method cellForRowAtIndexPath method within the UITableViewController assign that property to a block code like this (this code is at the same level of cell.index = indexPath;):

// configure the cell with the current indexPath
cell.updateModelOnEvent = ^(DataItem* dataItem) {

    [self.model insertObject:dataItem atIndex:indexPath.row];
};

Could be a valid alternative? In this case, do I have to use copy or retain policy?

Thank you in advance.

  • 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-05-29T16:27:27+00:00Added an answer on May 29, 2026 at 4:27 pm

    Why not just use [UITableView indexPathForCell:] with a delegate?

    MyViewController.h

    @interface MyViewController : UITableViewController <MyTableViewCellDelegate>
    
    @end
    

    MyViewController.m

    @implementation MyViewController
    
    // methods...
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
       NSString *reuseIdentifier = @"MyCell";
       MyTableViewCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
       if (cell == nil)
           cell = [[[MyTableViewCell alloc] initWithMyArgument:someArgument reuseIdentifier:reuseIdentifier] autorelease];
    
        [cell setDelegate:self];
    
        // update the cell with your data
    
        return cell;
    }
    
    - (void)myDelegateMethodWithCell:(MyTableViewCell *)cell {
    
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
        // update model
    }
    
    @end
    

    MyTableViewCell.h

    @protocol MyTableViewCellDelegate;
    
    @interface MyTableViewCell : UITableViewCell
    
    @property (assign, nonatomic) id <MyTableViewCellDelegate> delegate;
    
    - (id)initWithMyArgument:(id)someArgument reuseIdentifier:(NSString *)reuseIdentifier;
    
    @end
    
    
    @protocol MyTableViewCellDelegate <NSObject>
    
    @optional
    
    - (void)myDelegateMethodWithCell:(MyTableViewCell *)cell;
    
    @end
    

    MyTableViewCell.m

    @implementation MyTableViewCell
    
    @synthesize delegate = _delegate;
    
    - (id)initWithMyArgument:(id)someArgument reuseIdentifier:(NSString *)reuseIdentifier {
    
        self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    
        if (self) {
            // custom setup
        }
    
        return self;
    }
    
    - (void)prepareForReuse {
        [super prepareForReuse];
    
        self.delegate = nil;
    }
    
    - (void)someActionHappened {
    
        if ([self.delegate respondsToSelector:@selector(myDelegateMethodWithCell:)])
            [self.delegate myDelegateMethodWithCell:self];
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I

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.