嗨,在我的项目中,我需要从json中提取数据并插入tableview.i创建了两个单元格,但即使在正确的插座连接后它也不会出现。我不知道我做了什么错误。我的tableview和单元格不可见请检查我的编码,下面也有json电话。
-(void)WebServiceTopic{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
AFHTTPRequestOperationManager *TopicManager=[AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer*serializer=[AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
TopicManager.requestSerializer=serializer;
NSString *Postlink = [NSString stringWithFormat:@"%@Discussions/%@/?categoryid=14",PUBLICURL, [[NSUserDefaults standardUserDefaults]valueForKey:@"USERID"]];
[TopicManager GET:Postlink parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"JSON:%@", responseObject);
NSMutableDictionary *userDict=(NSMutableDictionary *)responseObject;
TopicArray = (NSMutableArray*)userDict;
[TopicTable reloadData];
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"the failure is %@", error);
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
}
#pragma mark -
#pragma mark UITableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
if (TopicArray.count==0) {
return TopicArray.count;
}
return TopicArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row==0)
{
static NSString *CellIdentifier1 = @"Cell";
DiscussTopicCell *cell = (DiscussTopicCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (TopicArray.count>0) {
NSDictionary *ResDiction = [TopicArray objectAtIndex:indexPath.row];
cell.ActTitle.text = [ResDiction objectForKey:@"Title"];
cell.Name.text=[ResDiction objectForKey:@"CreateUserName"];
cell.Comment.text=[ResDiction objectForKey:@"CommentCount"];
cell.Notes.text=[ResDiction objectForKey:@"Content"];
cell.Category.text=[ResDiction objectForKey:@"Category"];
cell.Time.text= [ResDiction objectForKey:@"CreateDate"];
UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-3,cell.frame.size.width,3)];
additionalSeparator.backgroundColor = [UIColor grayColor];
[cell addSubview:additionalSeparator];
}
return cell;
}
else
{
static NSString *CellIdentifier2 = @"Cell2";
DiscussTopicCell *cell = (DiscussTopicCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
NSDictionary *ResDiction = [TopicArray objectAtIndex:indexPath.row];
cell.ActTitle.text = [ResDiction objectForKey:@"Title"];
cell.Name.text=[ResDiction objectForKey:@"CreateUserName"];
cell.Comment.text=[ResDiction objectForKey:@"CommentCount"];
cell.Notes.text=[ResDiction objectForKey:@"Content"];
cell.Category.text=[ResDiction objectForKey:@"Category"];
cell.Time.text= [ResDiction objectForKey:@"CreateDate"];
UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-3,cell.frame.size.width,3)];
additionalSeparator.backgroundColor = [UIColor grayColor];
[cell addSubview:additionalSeparator];
return cell;
}
}
答案 0 :(得分:0)
您必须在从服务器提取数据时重新加载tableview,然后只显示tableview上的一些数据。请重新加载tableview。
参考示例代码
cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.Language_label.text=[[lang_array objectAtIndex:indexPath.row] objectForKey:@"lang_desc"];
if (indexPath.row == color_tag)
{
cell.Language_label.backgroundColor=[self colorWithHexString:@"FFCA13"];
}
else
{
cell.Language_label.backgroundColor=[UIColor whiteColor];
}
return cell;
}
答案 1 :(得分:0)
您需要创建单元格。如果没有已创建且可供使用的单元格,则可重复使用的单元格将返回nil。
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}