Ios UITableView单元格不可见

Ios UITableView单元格不可见,ios,uitableview,cell,mbprogresshud,Ios,Uitableview,Cell,Mbprogresshud,您好,在我的项目中,我需要从json中提取数据并插入tableview。我已经创建了两个单元格,但即使在正确的插座连接后,它也不会出现。我不知道我犯了什么错误。我的tableview和单元格不可见,请检查我下面的编码,其中也有json调用 -(void)WebServiceTopic{ [MBProgressHUD showHUDAddedTo:self.view animated:YES]; AFHTTPRequestOperationManager *TopicManage

您好,在我的项目中,我需要从json中提取数据并插入tableview。我已经创建了两个单元格,但即使在正确的插座连接后,它也不会出现。我不知道我犯了什么错误。我的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;

    }

}

当tableview从服务器中提取数据时,必须重新加载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;
}

您需要创建单元格。如果没有已创建并可供使用的单元,则可重用单元的出列将返回nil

if(!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}

您必须在收到响应后重新加载已注释的tableview。我这样做了,但它挂起了…现在仍然显示尝试删除(DiscussTopicCell*),位于DiscussTopicCell*cell=(DiscussTopicCell*)[tableview dequeueReusableCellWithIdentifier:CellIdentifier1]行;和其他.Yo需要在何时从web服务获取所有数据时实现
reloadData()
如果我删除了您不能调用存储在discussiontopiccell@Dev.RkNo中的数据,则即使在重新加载后也不会显示它…如果我这样做了,我的viewcontroller会挂断,我有疑问!你把单元格的标识符放在情节提要或xib中了吗?是的,两个单元格的名称是cell和cell2,请参见我的customcell代码,以供参考。