【问题标题】:Objective-C: 2 UItableViews displayedObjective-C:显示 2 个 UItableView
【发布时间】:2012-02-14 14:15:00
【问题描述】:

我很抱歉粘贴了一大段代码,但我真的很想让它工作; 这个应用程序应该在屏幕上放置一个 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";

}

【问题讨论】:

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


    【解决方案1】:

    你在使用界面生成器吗?然后检查您是否没有在 IB 中将 tableview 连接到控制器两次 - 一次作为 view 出口,再次作为 table

    【讨论】:

    • 感谢我将表格连接到数据源和委托,以及“UITableview 表格”,因为我想放置页脚。我的代码是这样的...... self->table.tableFooterView = bottomView;但是如果 IB 中的表格失去了与“表格”的连接,则页脚不会出现。不知道如何指定表格。
    • 您可能将表连接为控制器的 view 属性。你可以使用它。
    • 谢谢,我会使用 UIView。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多