在我的应用程序中,我想在一个具有分段选择的tableview中显示两个自定义单元格。当用户单击segment = 0时,显示第一个自定义单元格,segment = 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}>)未能从dataSource()'
获取一个单元格在这里,我没有实现这种方法,这种方法不会在我的tableview中显示数据。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (value1 == 0)
{
return 102;
}
else if(value1 == 1)
{
return 193;
}
return 0;
}
答案 0 :(得分:1)
您已在nil
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"];
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;
}