问题
I used the Json array to show data from server.
when I change the CellIdentifier to @Cell which i used in CellIdentifier the pushDetailView is working and it's going to next page but when I change to CustomCell again just show the name of city and state in first page and when i click on that doesnt go to next page. the reason I need the @CustomCell because I need 2 labels view in first page and when I change it to @cell it just show one label.
Thank you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
City * cityObject;
cityObject = [ citiesArry objectAtIndex:indexPath.row];
cell.textLabel.text = cityObject.cityState;
cell.detailTextLabel.text = cityObject.cityPopulation;
cell.detailTextLabel.numberOfLines = 4;
//NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://mobileapps.binaryappdev.com/applelogo.png"] ];
//[[cell imageView] setImage:[UIImage imageWithData:imageData] ];
return cell;
}
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"pushDetailView"])
{
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
City * object = [citiesArry objectAtIndex: indexPath.row];
[[segue destinationViewController] getCity:object];
}
}
#pragma mark -
#pragma mark Class Methods
-(void) retrievedData;
{
NSURL * URL = [ NSURL URLWithString:getDataURL];
NSData * data = [ NSData dataWithContentsOfURL:URL];
jsonArry = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:Nil];
//city
citiesArry = [[NSMutableArray alloc] init];
//loop
for (int i=0; i<jsonArry.count; i++) {
NSString * cID = [[ jsonArry objectAtIndex:i] objectForKey:@"id"];
NSString * cName = [[ jsonArry objectAtIndex:i] objectForKey:@"cityName"];
NSString * cState = [[ jsonArry objectAtIndex:i] objectForKey:@"cityState"];
NSString * cCountry = [[ jsonArry objectAtIndex:i] objectForKey:@"country"];
NSString * cPopulation = [[ jsonArry objectAtIndex:i] objectForKey:@"cityPopulation"];
NSString * cImage = [[ jsonArry objectAtIndex:i] objectForKey:@"cityImage"];
[citiesArry addObject:[[City alloc] initWithcityName:cName andcityState:cState andcityCountry:cCountry andcityPopulation:cPopulation andcityImage:cImage andcityID:cID ]];
}
[self.tableView reloadData];
}
@end
DetailViewController
@synthesize cityNameLabel, stateLabel, countryLabel, populationLabel, currentCity;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setLabels];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark Class Methods
-(void) getCity:(id)cityObject
{
currentCity = cityObject;
}
-(void) setLabels
{
cityNameLabel.text= currentCity.cityName;
countryLabel.text = currentCity.cityCountry;
stateLabel.text = currentCity.cityState;
populationLabel.text = currentCity.cityPopulation;
cityNameLabel.numberOfLines= 0;
}
@end
回答1:
if you don´t have segue for your customcell when you push in a cell your method didSelectRow:AtIndexPath will be called. You can do yourself a pushViewController from that method or [self performSegueWithIdentifier: @"pushDetailView" sender: self];
来源:https://stackoverflow.com/questions/22065940/define-the-uitableviewcell-out-of-storyboard-with-nib