I have an application in Xcode 4.6 which uses storyboards. I added a UITableView to a view controller class, which worked as expected. However, when I tried deleting the UITable
When you register a class, and use dequeueReusableCellWithIdentifier:forIndexPath:, the dequeue method is guaranteed to return a cell, so your if (cell == nil) clause is never entered. So, just do it the old way, don't register the class, and use dequeueReusableCellWithIdentifier:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"newFriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//etc.
return cell;
}
As for the segue, it can't be called because you can't make a segue to a table that you've created in code, not in IB. Again, go back to the old way and use tableView:didSelectRowAtIndexPath: which will be called when you select a cell. Instantiate your detail controller there and do the trasition in code.
After edit:
I didn't see your added code there. You've implemented didDeselectRowAtIndexPath rather than didSelectRowAtIndexPath. If you change that, your segue should work.
vc.m
#import "ViewController.h"
@interface ViewController ()
{
NSArray *cityArray;
NSArray *citySubTitleArray;
NSArray *cityImage;
NSInteger selectindexpath;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
cityArray = [[NSArray
alloc]initWithObjects:@"Coimbatore",@"Salem",@"Chennai",nil];
citySubTitleArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil];
cityImage = [[NSArray alloc]initWithObjects:@"12-300x272.png"
, @"380267_70d232fc33b44d4ebe7b42bbe63ee9be.png",@"apple-logo_318
-40184.png", nil];
}
#pragma mark - UITableView Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection
:(NSInteger)section
{
return cityImage.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"city";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell.textLabel.text = [cityArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [citySubTitleArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:
[cityImage objectAtIndex:indexPath.row]];
// NSData *data = [[NSData alloc]initWithContentsOfURL:
[NSURL URLWithString:@""]];
// cell.imageView.image = [UIImage imageWithData:data];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath
: (NSIndexPath *)indexPath
{
NSLog(@"---- %@",[cityArray objectAtIndex:indexPath.row]);
NSLog(@"----- %@",[cityImage objectAtIndex:indexPath.row]);
selectindexpath=indexPath.row;
[self performSegueWithIdentifier:@"second" sender:self];
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little p
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:@"second"])
{
secondViewController *object=segue.destinationViewController;
object.cityName=[cityArray objectAtIndex:selectindexpath];
object.cityImage=[cityImage objectAtIndex:selectindexpath];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
vc.m
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource
, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *cityLabelList;
@end
sv.m
#import <UIKit/UIKit.h>
@interface secondViewController : UIViewController
@property(strong, nonatomic) NSString *cityName;
@property(strong,nonatomic)NSString *cityImage;
@end
sv.h
#import "secondViewController.h"
@interface secondViewController ()
@property (strong, nonatomic) IBOutlet UILabel *lbl_desc;
@property (strong, nonatomic) IBOutlet UIImageView *img_city;
@end
@implementation secondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=self.cityName;
if ([self.cityName isEqualToString:@"Coimbatore"])
{
self.lbl_desc.text=@"Coimbatore city";
self.img_city.image=[UIImage imageNamed:
[NSString stringWithFormat:@"%@",self.cityImage]];
}
else if ([self.cityName isEqualToString:@"Chennai"])
{
self.lbl_desc.text= @"Chennai City Gangstar";
self.img_city.image=[UIImage imageNamed:
[NSString stringWithFormat:@"%@",self.cityImage]];
}
else
{
self.lbl_desc.text= @"selam City";
self.img_city.image=[UIImage imageNamed:
[NSString stringWithFormat:@"%@",self.cityImage]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidLoad {
[super viewDidLoad];
arr=[[NSArray alloc]initWithObjects:@"ABC",@"XYZ", nil];
tableview = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
tableview.delegate = self;
tableview.dataSource = self;
[self.view addSubview:tableview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
}
cell.textLabel.text=[arr objectAtIndex:indexPath.row];
return cell;
}