使用NIB定义故事板中的UITableViewCell

时间:2014-02-27 10:37:29

标签: ios json uitableview ios7 nib

我使用Json数组来显示来自服务器的数据。

当我将CellIdentifier更改为我在CellIdentifier中使用的@Cell时,pushDetailView正在运行,它将进入下一页但是当我再次更改为CustomCell时,只需在第一页显示城市和州的名称,当我点击该页面时不要去下一页。我需要@CustomCell的原因是因为我需要在第一页中使用2个标签视图,当我将其更改为@cell时,它只显示一个标签。

谢谢。

- (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 个答案:

答案 0 :(得分:0)

如果你在推入一个单元格时没有为你的自定义单元格设置segue你的方法didSelectRow:将调用AtIndexPath。你可以自己从这个方法做一个pushViewController或[self performSegueWithIdentifier:@“pushDetailView”sender:self];