objective c - iOS - How to present different scenes from UITableViewCell -
i have uiviewcontroller tableview. tableview populated image , text each cell. need present others scenes when user taps cell. example:
- tap on first row --> present viewcontroller1
- tap on second row --> present viewcontroller2
viewcontroller1 , viewcontroller2 scenes in storyboard.
i've tried various solutions, none of these works. seems method didselectrowatindexpath: not called when tap cell (for example tried show alert).
here code of viewcontroller: viewcontroller.h
#import <uikit/uikit.h> @interface viewcontroller : uiviewcontroller <uitableviewdelegate, uitableviewdatasource> @end viewcontroller.m
#import "viewcontroller.h" @interface viewcontroller () @end @implementation viewcontroller { nsarray *recipes; } - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. recipes = [nsarray arraywithobjects:@"news", @"calendar",@"take photo",@"draw",@"mail us",@"follow on facebook", nil]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [recipes count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *simpletableidentifier = @"simpletablecell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:simpletableidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:simpletableidentifier]; } cell.textlabel.text = [recipes objectatindex:indexpath.row]; if ([cell.textlabel.text isequaltostring:recipes[0]]) { cell.imageview.image = [uiimage imagenamed:@"newsicon"]; } else if ([cell.textlabel.text isequaltostring:recipes[1]]) { cell.imageview.image = [uiimage imagenamed:@"mensaicon"]; } else if ([cell.textlabel.text isequaltostring:recipes[2]]) { cell.imageview.image = [uiimage imagenamed:@"fotoicon"]; } else if ([cell.textlabel.text isequaltostring:recipes[3]]) { cell.imageview.image = [uiimage imagenamed:@"drawicon"]; } else if ([cell.textlabel.text isequaltostring:recipes[4]]) { cell.imageview.image = [uiimage imagenamed:@"mailicon"]; } else if ([cell.textlabel.text isequaltostring:recipes[5]]) { cell.imageview.image = [uiimage imagenamed:@"fbicon"]; } return cell; } -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // here cell selected row. uitableviewcell *selectedcell=[tableview cellforrowatindexpath:indexpath]; if ([selectedcell.textlabel.text isequaltostring:recipes[0]]) { uialertview *messagealert = [[uialertview alloc] initwithtitle:@"row selected" message:@"you've selected row" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; // display alert message [messagealert show]; nsstring *storyboardname = @"main"; uistoryboard *storyboard = [uistoryboard storyboardwithname:storyboardname bundle: nil]; uiviewcontroller *vc = [storyboard instantiateviewcontrollerwithidentifier:@"news"]; [self presentviewcontroller:vc animated:yes completion:nil]; } // use image in next window using view controller instance of view. } @end i'm new ios developing, don't know if code right or if there other solutions more elegant of these. anyway, let's focus on problem, can me?
if view table view i'd suggest using uitableviewcontroller instead of uiviewcontroller. if have uitableview in existing uiviewcontroller need set delegate , data source methods yourself.
can in storyboard. click on tableview , select connections inspector. click circle next datasource , drag view controller. same delegate. why table view methods aren't being called.
if it's static table, can create independent segues each cell in storyboard.
Comments
Post a Comment