如何在ios中设置tableView单元格标题?
我正在解析JSON并将其存储在一个数组中。我有一个自定义的cell子类,在该子类中,我试图根据解析的JSON内容设置标签的标题。 这就是解析过程如何在ios中设置tableView单元格标题?,ios,uitableview,nsdictionary,Ios,Uitableview,Nsdictionary,我正在解析JSON并将其存储在一个数组中。我有一个自定义的cell子类,在该子类中,我试图根据解析的JSON内容设置标签的标题。 这就是解析过程 @implementation BlogScreenViewController - (void)viewDidLoad { [super viewDidLoad]; self.jsonArray = [[NSMutableArray alloc]init]; NSError *error = nil; NSURL *url =
@implementation BlogScreenViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.jsonArray = [[NSMutableArray alloc]init];
NSError *error = nil;
NSURL *url = [NSURL URLWithString:@"http:web_services/blog.php"];
NSData *data = [NSData dataWithContentsOfURL:url];
self.jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
for (NSDictionary *obj in self.jsonArray)
{
NSLog(@"JSON string output :- %@ ", [obj objectForKey:@"titre"]); // shows titles
}
在自定义单元格子类中
// set blog title label
UILabel *blogTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 20, 200, 20)];
// blogTitleLabel.text = @"This is blog title"; // This shows properly if used.
for (NSDictionary *obj in blogScreenVC.jsonArray ) //This doesn't work.
{
blogTitleLabel.text = [NSString stringWithFormat:@"%@",[obj objectForKey:@"titre"]];
}
[blogTitleLabel setFont:[UIFont systemFontOfSize:9]];
[self addSubview:blogTitleLabel];
不要在customcell类中设置lebel.text,而是将其添加到
cellforrowatinexpath
中
在customcell.h
文件中创建标签的属性
@property (nonatomic, weak) IBOutlet UILabel *label;
#import "CustomCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.jsonArray.count !=0) {
NSMutableDictionary * tmpDictn = [self.jsonArray objectAtIndex:indexPath.row];
if ([tmpDictn objectForKey:@"titre"] != nil)
{
cell.label.text = [tmpDictn objectForKey:@"titre"];
}else{
cell.label.text = @"tmpDictn does not contain data";
}
}else{
cell.label.text = @"jsonArray does not contain data";
}
return cell;
}
并在customcell.m
文件中对其进行合成
@synthesize label = _label;
在您的viewcontroller.m
文件中
@property (nonatomic, weak) IBOutlet UILabel *label;
#import "CustomCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.jsonArray.count !=0) {
NSMutableDictionary * tmpDictn = [self.jsonArray objectAtIndex:indexPath.row];
if ([tmpDictn objectForKey:@"titre"] != nil)
{
cell.label.text = [tmpDictn objectForKey:@"titre"];
}else{
cell.label.text = @"tmpDictn does not contain data";
}
}else{
cell.label.text = @"jsonArray does not contain data";
}
return cell;
}
你在这件事上面临的问题是什么?把你的问题放清楚?您的问题标题谈论tableview标题。但在你的问题中,身体会告诉你细胞的名字吗?请清楚地填写一些说明。在自定义单元格类中,将自定义单元格标签obj作为属性综合,并在表视图委托方法中使用该obj。@Romance-请提供任何示例代码以了解您的方法?@icodes请参阅下面的答案,该答案由dilep回答。它将解决您的问题,请按照下面的思路进行操作。