M
M
Maxim Prigozhenkov2015-01-21 22:21:42
iOS
Maxim Prigozhenkov, 2015-01-21 22:21:42

How to pass data from UITableViewCell to UIViewController?

Good afternoon))
The problem is this: there is a cell in the TableView that contains one UIImageView, one Label and a button. By clicking on the button, a transition is made to another UIViewController.
Question: how to pass the data that is in the cell to the UIViewController when switching to it after clicking the button?
I just can’t figure it out, I tried somehow to transfer it from the cell class through Segue, it doesn’t work.
Yes, the cell has its own class.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander D., 2015-01-23
@Waka_Waka

Analyze your architecture, if you need to receive data from a cell - this is, unfortunately, an architectural error. Cells are intended only for displaying data from the data source (actually, the datasouce of the collection view). If, after "clicking" on the button, or segue occurs, then the cell index can be determined:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//не забыть проверить, кто такой sender
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
//получить данные из datasource
}

The main thing to understand is that the cell is intended only for displaying data, all the logic of the data source is either in the controller or in a separate delegate (which is better, of course)

O
one pavel, 2015-01-21
@onepavel

Without seeing your code, I guess you should try some more work with segue
Dear Ray posted a good tutorial on storyboards
www.raywenderlich.com/50310/storyboards-tutorial-i...
I haven't worked much with storyboards, more old fashioned with xib' ami
but the transmission should be as in Ray's example

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"PickGame"]) {
        GamePickerViewController *gamePickerViewController = segue.destinationViewController;
        gamePickerViewController.delegate = self;
        gamePickerViewController.game = _game;
    }
}

A
An, 2015-01-22
@Flanker_4

Usually this is done through delegation
Here is an example
stackoverflow.com/questions/8062572/adding-a-deleg ... 4910323/delegate-from-... And pass the element number through the button tag. Although it will turn out perverted ... Another option is blocks, if you don’t like delegation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question