0

私は3つの文字列を持っており、それぞれがTrueまたはFalseを示しています。どの文字列がTrueであるかに応じて、テーブルビューに表示します。これが私が持っているコードです:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  if ([appDelegate.showFirstField isEqualToString:@"True"]) {
    if (indexPath.section==2) {
        cell.textLabel.text=@"First Field Title";
    }
  }

  if ([appDelegate.showSecondField isEqualToString:@"True"]) {
    if (indexPath.section==3) {
        cell.textLabel.text=@"Second Field Title";
    }
  }

  if ([appDelegate.showThirdField isEqualToString:@"True"]) {
    if (indexPath.section==4) {
        cell.textLabel.text=@"Third Field Title";
    }
  }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   //#warning Potentially incomplete method implementation.
   // Return the number of sections.
   int sectionNum = 2;
   if ([appDelegate.showFirstField isEqualToString:@"True"]) {
     sectionNum += 1;
   }
   if ([appDelegate.showSecondField isEqualToString:@"True"]) {
     sectionNum += 1;
   }
   if ([appDelegate.showThirdField isEqualToString:@"True"]) {
     sectionNum += 1;
   }
   return sectionNum;
}

問題は、showFirstFieldshowThirdFieldがTrueでshowSecondFieldFalseの場合、セクションタイトルが正しく設定されないことです。タイトルを次の利用可能なセクションに設定する方法はありますか?

4

2 に答える 2

0

あなたのコードでは、 showFirstFieldandshowThirdFieldがTrueでshowSecondFieldFalseの場合、 4sectionNumになります。したがって、の可能な値は0、1、2 、および3になります。このコードではindexPath.section

if ([appDelegate.showThirdField isEqualToString:@"True"]) {
    if (indexPath.section==4) {
        cell.textLabel.text=@"Third Field Title";
    }
  }

この条件[appDelegate.showThirdField isEqualToString:@"True"]が真であっても、他の条件indexPath.section==4は偽になります。

だからあなたの

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   //#warning Potentially incomplete method implementation.
   // Return the number of sections.
   int sectionNum = 2;
   if ([appDelegate.showFirstField isEqualToString:@"True"]) {
     sectionNum += 1;
   }
   if ([appDelegate.showSecondField isEqualToString:@"True"]) {
     sectionNum += 1;
   }
   if ([appDelegate.showThirdField isEqualToString:@"True"]) {
     sectionNum += 1;
   }
   return sectionNum;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{  
   return 5;
}
于 2012-07-21T12:56:06.270 に答える
0

それに取り組んだ数時間後、私は別のアプローチからそれを取りました、そしてこのコードは私のために働きます。ブール値をfalseに設定し、グローバル変数として設定しますcheck

if ([appDelegate.showFirstField isEqualToString:@"True"] && checkFirst == FALSE){
    if (indexPath.section >1 && indexPath.section==indexPath.section) {
        checkFirst = TRUE;
        cell.textLabel.text=@"First Field Title";                
    }
}
else if ([appDelegate.showSecondField isEqualToString:@"True"] && checkSecond == FALSE){
    if (indexPath.section >1 && indexPath.section==indexPath.section) {
        checkSecond = TRUE;
        cell.textLabel.text=@"Second Field Title";
    }
}
else if ([appDelegate.showThirdField isEqualToString:@"True"] && checkThird == FALSE){
    if (indexPath.section >1 && indexPath.section==indexPath.section) {
        checkThirdField = TRUE;
        cell.textLabel.text=@"Third Field Title";
    }
}
于 2012-07-26T19:45:51.177 に答える