【发布时间】:2016-06-05 12:31:45
【问题描述】:
我正在创建 UITableview 并添加两个 UITableviewCell 自定义单元格(ProductCell 和 DescriptionCell)。我正在将数据显示到 UILabel 中。但我收到此错误([__NSCFString objectAtIndex:]: unrecognized selector sent to instance)并且 UITableviewCell 不显示数据。 请检查我的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
[self.detailedTableview registerNib:[UINib nibWithNibName:@"ProductCell" bundle:nil] forCellReuseIdentifier:@"ProductCell"];
[self.detailedTableview registerNib:[UINib nibWithNibName:@"DescriptionCell" bundle:nil] forCellReuseIdentifier:@"DescriptionCell"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.detailedTableview])
{
if (indexPath.section == 0)
{
return 396;
}
if (indexPath.section == 1)
{
// return UITableViewAutomaticDimension;
return 100;
}
}
else
{
if (indexPath.section == 0)
{
return 396;
}
if (indexPath.section == 1)
{
// return UITableViewAutomaticDimension;
return 100;
}
}
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifierproduct = @"ProductCell";
static NSString *cellIdentifierDescription = @"DescriptionCell";
if (indexPath.section == 0) {
ProductCell *cellProduct =[tableView dequeueReusableCellWithIdentifier:cellIdentifierproduct];
return cellProduct;
}
else if (indexPath.section ==1)
{
DescriptionCell *cellDes =[tableView dequeueReusableCellWithIdentifier:cellIdentifierDescription];
NSString*urlString = [NSString stringWithFormat:@"http://54.254.171.25/android/bnm/api/servicesAPI.php?request=getProductDetails&pid=1"];
NSURL *url=[NSURL URLWithString:urlString];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *dataDictionary=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *descArray =[dataDictionary objectForKey:@"pd_description"];
NSLog(@"%@",descArray);
cellDes.descriptionLbl.text = [descArray objectAtIndex:indexPath.row];
return cellDes;
}
return nil;
}
请检查
【问题讨论】:
-
为什么要在
cellForRowAtIndexPath方法中下载数据并解析JSON?这需要完成一次,然后将生成的数据用作表格视图的数据源。 -
这行
NSLog(@"%@",descArray);在崩溃前打印什么? -
在 descArray 中打印了这个(亚麻混纺材料。常规衣领。全袖。修身。纯色图案。桃色)
-
请阅读一些关于模型-视图-控制器模式的内容。 controller 将数据加载到 model 并更新 view。您正在 view 中加载数据,这太可怕了。
标签: ios objective-c uitableview