如何使用分段控件在一个 table 视图中维护两个自定义单元格?

How to maintain two custom cells in one table view with segmented control?

在我的应用程序中,我想在一个带有段的表视图中显示两个自定义单元格 selection.when 用户单击段=0 显示第一个自定义单元格,段=1 显示第二个自定义单元格。 这是我的代码。

-(void)callSegmentSelected
{
value = (int)segment.selectedSegmentIndex;
if (segment.selectedSegmentIndex == 0)
{
        NSString *UDID = [[NSUserDefaults standardUserDefaults]objectForKey:@"kPrefKeyForUserID"];
        NSString *catId = [[NSUserDefaults standardUserDefaults]objectForKey:@"kPrefKeyForCatID"];
        NSString *sessionKey = @"akjsda9s8dadandlakd9";
        NSString *urlString = [NSString stringWithFormat:@"API"];
        NSString *jsonString = [NSString stringWithFormat:GETCONTEST,catId,UDID,sessionKey];
        NSData *myJSONData =[jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];
        NSMutableData *body = [NSMutableData data];
        [body appendData:[NSData dataWithData:myJSONData]];
        [request setHTTPBody:body];
        NSError *error;
        NSURLResponse *response;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        if(str.length > 0)
        {
            NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error;
            NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]);
            fldExamTitleArray =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:[NSString stringWithFormat:@"%d", [[[fldExamTitleArray objectAtIndex:0] valueForKey:@"examid"] intValue]] forKey:@"kPrefKeyForExamID"];
            NSLog(@"Examid: %@", [defaults objectForKey:@"kPrefKeyForExamID"]);
            [defaults synchronize];
            [progressHud hide:YES];
            [self.myContestTableView reloadData];
        }
        else
        {
            NSLog(@"Error");
        }
}
else if (segment.selectedSegmentIndex == 1)
{
}
else if (segment.selectedSegmentIndex == 2)
{
}

这里是 cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (value == 0)
{
    static NSString *cellId = @"Cell";
    ContestTableViewCell * cell = (ContestTableViewCell *)[myContestTableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        NSArray *myNib;
        myNib = [[NSBundle mainBundle]loadNibNamed:@"ContestTableViewCell" owner:self options:nil];
        cell = [myNib objectAtIndex:0];
    }
    cell.fldExamTitleLbl.text = [[fldExamTitleArray objectAtIndex:indexPath.row]valueForKey:@"fldexamtitle"];
}
else
{
    if (value == 1)
    {
        static NSString * cellIdentifier = @"Cell2";
        ContestRewardCell *cell = (ContestRewardCell *)[myContestTableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil)
        {
            NSArray *myNib;
            myNib = [[NSBundle mainBundle]loadNibNamed:@"ContestRewardCell" owner:self options:nil];
            cell =[myNib objectAtIndex:0];
        }
        cell.examTitleLbl.text = [[fldExamTitleArray objectAtIndex:indexPath.row]valueForKey:@"fldexamtitle"];
}
return nil;
}

我收到这个错误,

由于未捕获的异常 'NSInternalInconsistencyException',正在终止应用程序,原因:'UITableView (; layer = ; contentOffset: {0, 0}; contentSize: {320, 468}>) failed to obtain a cell from its dataSource ()'

这里我没有实施这种方法,因为数据没有显示在我的表格视图中。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (value1 == 0)
{
    return 102;
}
else if(value1 == 1)
{
    return 193;
}
return 0;
}

您已在 cellForRowAtIndexPath

返回 nil

像下面这样使用:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (value == 0)
{
    static NSString *cellId = @"Cell";
    ContestTableViewCell * cell = (ContestTableViewCell *)[myContestTableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        NSArray *myNib;
        myNib = [[NSBundle mainBundle]loadNibNamed:@"ContestTableViewCell" owner:self options:nil];
        cell = [myNib objectAtIndex:0];
    }
    cell.fldExamTitleLbl.text = [[fldExamTitleArray objectAtIndex:indexPath.row]valueForKey:@"fldexamtitle"];
    return cell;
}
else
{
    if (value == 1)
    {
        static NSString * cellIdentifier = @"Cell2";
        ContestRewardCell *cell = (ContestRewardCell *)[myContestTableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil)
        {
            NSArray *myNib;
            myNib = [[NSBundle mainBundle]loadNibNamed:@"ContestRewardCell" owner:self options:nil];
            cell =[myNib objectAtIndex:0];
        }
        cell.examTitleLbl.text = [[fldExamTitleArray objectAtIndex:indexPath.row]valueForKey:@"fldexamtitle"];
        return cell;

}
return nil;
}