Objective-C:显示2个UItableViews

时间:2012-02-14 14:15:01

标签: iphone objective-c ios cocoa-touch uitableview

我很抱歉粘贴了大量代码,但我真的想让它工作; 这个应用程序应该在屏幕上放置UItableView。 不知何故,这段代码两次调用tableview方法并创建重复的表。

您是否有任何建议我可以采取哪些措施来解决此问题? 提前谢谢。

的UITableView:

    - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
 NSLog(@"-------------numberOfRowsInSection---------------");
 NSLog(@"qtext =%d",[qtext count]);
return [qtext count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return 1;
}

-(void)insertDeviders{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
myLabel.text = @"About you";
[cell addSubview:myLabel];
}
//Return cellheight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{

NSLog(@"-------------heightforrowatindexpath---------------"); 

dict = [qtext objectAtIndex:[indexPath row]];

NSDictionary  *dict2 = [qtype objectAtIndex:[indexPath row]];



type = [[dict2 allKeys] objectAtIndex:0]; 

NSString *text = [[dict allKeys] objectAtIndex:0]; 

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);


   thisheight= height + (CELL_CONTENT_MARGIN * 2) + 40;


   NSLog(@"thisheight=%d",thisheight);

NSString *q = [NSString stringWithFormat:@"%d", thisheight];   
[arrAllheight addObject:[NSString stringWithFormat:@"%@",q]];



if([type isEqualToString:@"devider"]){thisheight=28;}


NSLog(@"dict=%@",dict);
NSLog(@"thisheight=%d",thisheight);

return thisheight;



}

填充单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"-------------cellForRowAtIndexPath---------------");   

cell_id = [qid objectAtIndex:[indexPath row] ];       
static NSString *CellIdentifier = @"Cell";    
label = nil; 
 cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


//  if(![cell_id intValue]==0){     
dict = [qtext objectAtIndex:[indexPath row]];
celltext = [NSString stringWithFormat:@"%@\n\n\n",[[dict allKeys] objectAtIndex:0]];
dict = [qtype objectAtIndex:[indexPath row]];
type = [[dict allKeys] objectAtIndex:0];     

//place the question
cell.textLabel.text = celltext;  

  //  NSLog(@"row=%@",[indexpath row]);
    if([type isEqualToString:@"devider"]){      
        [self configureDevider];
    }else{
        [self configureCell];      
    }
    if([cell_id intValue] == ([qid count])){
    tabledone = @"Yes";
    }

tableView.backgroundColor=[UIColor clearColor];
tableView.opaque=NO;
tableView.backgroundView=nil;

NSString *a = [arrAllheight objectAtIndex:[indexPath row]];
// NSLog(@"allheight=%d",allheight);

allheight +=thisheight; 
thisheight =[a intValue];   


if(![tabledone isEqualToString:@"Yes"])
if([type isEqualToString:@"YN"]){

    DCRoundSwitch *ynSwitch = [[DCRoundSwitch alloc] initWithFrame:CGRectMake(220,thisheight-40,80,27)] ;  

    ynSwitch.onText=@"Yes";
    ynSwitch.offText=@"No";
    [answers addObject:ynSwitch];
    [cell addSubview:ynSwitch];
    [ynSwitch setTag:[cell_id intValue]]; 
    [ynSwitch addTarget:self  action:@selector(setAnswersForRoundSwitches:) forControlEvents:UIControlEventValueChanged];

           NSLog(@"cell_id=%@", [dicAnswers objectForKey:[NSString stringWithFormat:@"", cell_id]]);

//    if[dicAnswers objectForKey:[NSString stringWithFormat:@"", cell_id]]){}
    i++;


}else if([type isEqualToString:@"freetext"]){

    //When the done button was clicked, remove the keybords from the screen
    [self makeTextField];

    [rtxtfield addTarget:self  action:@selector(setAnswersfortextFields:) forControlEvents:UIControlEventEditingDidEndOnExit];
    //    [rtxtfield value]; 



}else if([type isEqualToString:@"dropdown"]){

    picc = [picker_array objectForKey:[[NSString alloc]  initWithFormat:@"%d",cell_id]];

    //Choose an array for this textField
    // [self getPickerArray];
    [self makeTextField];
    //[rtxtfield addTarget:self  action:@selector(setAnswersfortextFields:) forControlEvents:UIControlEventEditingDidEndOnExit];  

    //When the done button was clicked, remove the keybords from the screen
    [rtxtfield addTarget:self action:@selector(textFieldReturn:) forControlEvents:UIControlEventEditingDidEndOnExit];
    //Get the tag for picker
    [rtxtfield addTarget:self action:@selector(getTag:) forControlEvents:UIControlEventTouchDown];     
    //Display picker
    [rtxtfield addTarget:self action:@selector(acsheet:) forControlEvents:UIControlEventTouchDown];     
    //set Tag for the textField
    [rtxtfield setTag:[cell_id intValue]];
       NSLog(@"rtxtfield tag=%d",rtxtfield.tag);

}


if([type isEqualToString:@"devider"]){   
[self caliculateHeightofCell];
}else{
[self caliculateHeightofCell];
}  



return cell;

}

用户完成编辑后获取标签名称:

-(void)getTag:(UITextField*)txtf{
NSLog(@"-------------getTag-------------");
whichTextTag = txtf.tag;
picc = [picker_array objectForKey:[[NSString alloc]  initWithFormat:@"%d",whichTextTag]];
[self getPickerArray];
}

生命周期:

- (void)viewDidLoad
{
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"main_bg.png"]];
 //   [super viewDidLoad];

    dicAnswers = [NSMutableDictionary dictionary];
[self getClaimQuestions];
[self  getSchemeLimitThreshold];
[self getShchemeLimit];

    tabledone=@"";
[self getQuestionsAY];

 //   [self getQuestionsH]; 
[self getCountries];

//  [self getQuestions];
[self getITClass];
self->table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self->table.separatorColor = [UIColor lightGrayColor];


[self placeNextButton];
   // tabledone =@"Yes";

}

2 个答案:

答案 0 :(得分:1)

您使用的是界面构建器吗?然后检查您是否在IB中将表视图连接到控制器两次 - 一次作为视图插座再次作为

答案 1 :(得分:1)

我没有测试过您的代码,因为我没有您的数据,也没有看到它是如何构建的,但查看代码我发现了几个问题。最可怕的是你可能不明白dequeueReusableCellWithIdentifier应该如何运作。您似乎在cell之外定义变量tableView:cellForRowAtIndexPath:,这意味着您每次都可以覆盖该单元格。我建议你阅读dequeueReusableCellWithIdentifier应该如何工作的内容。看看这里:iPhone - What are reuseIdentifiers (UITableViewCell)?