我正在做一个测验项目,其中问题的数量和问题的选项数量是动态的(取自服务 url)。一个问题最多 5 个选项(但可能会减少)。我在分段的 UITableView 中显示它,每个问题及其选项显示在一个部分中。我将对象放在 NSMutableArray 的一个部分中并每次循环选项。但我只得到最后一个选项。我不明白为什么会这样?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *hlCellID=@"hlCellID";
UITableViewCell* hlcell=[tableView dequeueReusableCellWithIdentifier:hlCellID];
if(hlcell == nil)
{
hlcell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID]autorelease];
hlcell.accessoryType = UITableViewCellAccessoryNone;
hlcell.selectionStyle = UITableViewCellSelectionStyleNone;
lblQuestion = [[UILabel alloc] initWithFrame:CGRectZero];
lblQuestion.tag = 1;
lblQuestion.backgroundColor = [UIColor clearColor];
[lblQuestion setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
[lblQuestion setLineBreakMode:NSLineBreakByWordWrapping];
[hlcell addSubview:lblQuestion];
lblanswers = [[UILabel alloc] initWithFrame:CGRectZero];
lblanswers.tag = 2;
lblanswers.backgroundColor = [UIColor clearColor];
[lblanswers setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
[lblanswers setLineBreakMode:NSLineBreakByWordWrapping];
[hlcell addSubview:lblanswers];
}
int section=indexPath.section;
NSMutableArray *sectionItems=[self.finalarray objectAtIndex:indexPath.section];
int n=[sectionItems count];
hlcell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *question = [NSString stringWithFormat:@"%@",[[sectionItems objectAtIndex:indexPath.row] objectForKey:@"Question"]];
CGSize constraint1 = CGSizeMake(320, 2000.0f);
CGSize size1 = [question sizeWithFont: [UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];
lblQuestion = (UILabel *)[hlcell viewWithTag:1];
lblQuestion.text = [NSString stringWithFormat:@"%@",question];
[lblQuestion setNumberOfLines:0];
lblQuestion.frame = CGRectMake(10,25, size1.width, size1.height);
for(int j=1;j<n;j++)
{
NSLog(@"%@",[[sectionItems objectAtIndex:j] objectForKey:@"Description"]);
NSString *answer = [NSString stringWithFormat:@"%@",[[sectionItems objectAtIndex:j] objectForKey:@"Description"]];
CGSize constraint2 = CGSizeMake(320, 2000.0f);
CGSize size2 = [answer sizeWithFont: [UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint2 lineBreakMode:NSLineBreakByWordWrapping];
lblanswers = (UILabel *)[hlcell viewWithTag:2];
lblanswers.text = [NSString stringWithFormat:@"%@",answer];
[lblanswers setNumberOfLines:0];
lblanswers.frame = CGRectMake(10,20*j, size2.width, size2.height);
[hlcell.contentView addSubview:lblanswers];
}
return hlcell;
}
我不明白如何为 lblanswers 设置框架。
编辑:
-(IBAction)checkboxClicked:(id)sender
{
NSMutableArray *arr1=[[NSMutableArray alloc]init];
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
NSLog(@"The section is %d", indexPath.section);
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
int tagID=[sender tag];
int divnum=0;
if(tagID<100)
divnum=10;
else
divnum=100;
int section=[sender tag]/divnum;
section-=1;
int itemId=[sender tag]%divnum;
if(itemId==selectedrowforCheckBox)
{
if(self.isChecked ==NO)
{
self.isChecked =YES;
[sender setImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateNormal];
NSNumber* xWrapped = [NSNumber numberWithInt:itemId];
int m=[cellSection count]-1;
NSString *questionId=[[cellSection objectAtIndex:m-1]objectForKey:@"QId"];
NSMutableDictionary *hk=[[NSMutableDictionary alloc]init];
[hk setObject:xWrapped forKey:@"Ans"];
[hk setObject:questionId forKey:@"QId"];
[selectedOptionandQIdArray addObject:hk];
}
else
{
self.isChecked =NO;
[sender setImage:[UIImage imageNamed:@"checkbox_unchecked.png"]forState:UIControlStateNormal];
NSNumber* xWrapped = [NSNumber numberWithInt:itemId];
int m=[cellSection count]-1;
NSString *questionId=[[cellSection objectAtIndex:m-1]objectForKey:@"QId"];
for (int i=0;i<[selectedOptionandQIdArray count];i++)
{
NSString *qid=[[selectedOptionandQIdArray objectAtIndex:i]objectForKey:@"QId"];
if(xWrapped==[[selectedOptionandQIdArray objectAtIndex:i]objectForKey:@"Ans"] && [questionId isEqualToString:qid])
{
NSLog(@"nnd");
[selectedOptionandQIdArray removeObjectAtIndex:i];
}
}
}
selectedrowforCheckBox=itemId;
}
else
{
if(self.isChecked ==NO)
{
self.isChecked =YES;
[sender setImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateNormal];
NSNumber* xWrapped = [NSNumber numberWithInt:itemId];
int m=[cellSection count]-1;
NSString *questionId=[[cellSection objectAtIndex:m-1]objectForKey:@"QId"];
NSMutableDictionary *hk=[[NSMutableDictionary alloc]init];
[hk setObject:xWrapped forKey:@"Ans"];
[hk setObject:questionId forKey:@"QId"];
[selectedOptionandQIdArray addObject:hk];
}
else
{
self.isChecked =NO;
[sender setImage:[UIImage imageNamed:@"checkbox_unchecked.png"]forState:UIControlStateNormal];
}
selectedrowforCheckBox=itemId;
}
}