使用 NIB 从 Storyboard 中定义 UITableViewCell
Posted
技术标签:
【中文标题】使用 NIB 从 Storyboard 中定义 UITableViewCell【英文标题】:define the UITableViewCell out of Storyboard with NIB 【发布时间】:2014-02-27 10:37:28 【问题描述】:我使用 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
【问题讨论】:
显示当用户点击单元格时触发的代码。 @reecon 你的意思是detailview类? 单元标识符是您在情节提要中为单元设置的字符串,因此它将加载包含它的单元。对于自定义单元格,您必须对它们进行子类化并添加您自己的标签或您需要的任何其他内容。 @CalinChitu 感谢您的评论,您能给我示例代码吗?为 CustomCell 创建子类并将其添加到 Cell? 【参考方案1】:如果您在推入一个单元格时没有为您的 customcell 设置 segue,您的方法 didSelectRow:AtIndexPath 将被调用。您可以从该方法或 [self performSegueWithIdentifier: @"pushDetailView" sender: self];
【讨论】:
感谢您的评论。是的,但是当我将此代码添加到应用程序时,第一页更改为详细视图页面,然后当我尝试再次点击返回第一页时,第一页中的结果相同,我无法返回详细视图以上是关于使用 NIB 从 Storyboard 中定义 UITableViewCell的主要内容,如果未能解决你的问题,请参考以下文章
为从 Nib 或 Storyboard 生成的 WKWebView 设置自定义 WKWebViewConfiguration
在 Nib 而不是 Storyboard 中创建 UITabBarController
从情节提要加载的 nib 的 Cocoa replaceSubview