user973067
user973067

Reputation: 337

Objective-C: 2 UItableViews displayed

I am sorry for pasting a big chunk of code but i really want to get it work; This app is supposed to be placing a UItableView on a screen. Somehow this code is calling tableview methods twice and creating duplicate tables.

Do you have any suggestion what I can do to fix this behaviour? Thanks in advance.

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;



}

Populate cell:

- (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;

}

get tag name when user finished editing:

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

lifecycle:

- (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";

}

Upvotes: 0

Views: 184

Answers (2)

ragamufin
ragamufin

Reputation: 4162

I haven't tested your code as I don't have your data nor do I see how it's being built but looking through your code I see several problems. The most dire one though is you may not understand how dequeueReusableCellWithIdentifier is supposed to work. You appear to be defining the variable cell outside of your tableView:cellForRowAtIndexPath: which means that you could be overwriting that cell every time. I'd suggest you read up on how the dequeueReusableCellWithIdentifier is supposed to work. Have a look here: iPhone - What are reuseIdentifiers (UITableViewCell)?.

Upvotes: 1

Peter Sarnowski
Peter Sarnowski

Reputation: 11970

Are you using interface builder? Then check if you have not connected the tableview to the controller twice in IB - once as the view outlet and again as table.

Upvotes: 1

Related Questions