define the UITableViewCell out of Storyboard with NIB

笑着哭i 提交于 2019-12-12 02:25:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!