簡體   English   中英

使用NIB在Storyboard之外定義UITableViewCell

[英]define the UITableViewCell out of Storyboard with 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

如果在推入單元格時沒有為customcell進行搜索,則將調用方法didSelectRow:AtIndexPath。 您可以通過該方法或[self performSegueWithIdentifier:@“ pushDetailView” sender:self]自己創建一個pushViewController;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM