使用NIB从Storyboard中定义UITableViewCell [英] define the UITableViewCell out of Storyboard with NIB

查看:125
本文介绍了使用NIB从Storyboard中定义UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used the Json array to show data from server.

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

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.

谢谢。

- (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

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


推荐答案

如果在推入单元格时没有segue用于自定义单元格,则会调用方法didSelectRow:将调用AtIndexPath。您可以使用该方法自己执行pushViewController或[self performSegueWithIdentifier:@pushDetailViewsender:self];

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];

这篇关于使用NIB从Storyboard中定义UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆