点击时,其他部分中的细胞重复

时间:2016-06-15 07:17:29

标签: objective-c uitableview row cell-array section508

我在每个部分有四个部分,有五行。当点击任何一行时,它会再次点击时显示实际数据,它会显示另一个部分数据。我无法理解我错在哪里。重复数据在另一行中。这是我的代码和图片。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (section==0) {
    return 5;
}
if (section==1) {
    return 5;
}
if (section==2) {
    return 5;
}
if (section==3) {
    return 5;
}


return 5;
}

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{



  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
headerView.tag = section;



headerView.backgroundColor = [UIColor lightTextColor];

 UILabel *headerString= [[UILabel alloc] initWithFrame:CGRectMake(10,    0, self.view.frame.size.width-20-50,50)];

BOOL manyCells = [[boolenarry objectAtIndex:section] boolValue];

if (!manyCells) {
    headerString.text =self.Titlearray[section]
    }else{
    headerString.text =self.Titlearray[section];


}

 headerString.textAlignment      = NSTextAlignmentLeft;
 headerString.textColor          = [UIColor whiteColor];
[headerView addSubview:headerString];

  UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer    alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[headerView addGestureRecognizer:headerTapped];

return headerView;
     }
    - (UIView *)tableView:(UITableView *)tableView 
 viewForFooterInSection:(NSInteger)section{
UIView *footer  = [[UIView alloc] initWithFrame:CGRectZero];
return footer;
 }

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

return 2;
  }
  - (CGFloat)tableView:(UITableView *)tableView   heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  if ([[boolenarry objectAtIndex:indexPath.section] boolValue]) {
    return 50;


  }




    return 0;
  }



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



   static NSString *CellIdentifier= @"Cell";

   UITableViewCell *cell = [tableView   dequeueReusableCellWithIdentifier:CellIdentifier];



       if (cell == nil) {

      cell = [[UITableViewCell alloc]    initWithStyle:
  UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == 
    UIUserInterfaceIdiomPhone) {

    }
}


   BOOL manyCells  = [[boolenarry objectAtIndex:indexPath.section] 
   boolValue];
   if (!manyCells) {


    NSDate *object = self.Titlearray[indexPath.section];
    cell.textLabel.text = [object description];
 }
 else{
        NSArray *content = [recipes valueForKey:[Titlearray 
 objectAtIndex:indexPath.section]];
cell.textLabel.text = [content objectAtIndex:indexPath.row];
     }


    if (indexPath.section==0) {

    allindgre= [recipes objectForKey:@"Ingrdent"];
    preparetime=[recipes objectForKey:@"preparetimeing"];
    makingarray=[recipes objectForKey:@"makingsample"];


      imagearray=[[NSMutableArray alloc]initWithObjects:[UIImage  
     imageNamed:@"chickengravy1.jpg"],[UIImage 
 imageNamed:@"Crispychicken.jpg"],[UIImage 
 imageNamed:@"ButtermilkChk.jpg"],

              [UIImage imageNamed:@"southernchik.jpg"],
               [UIImage imageNamed:@"SimpleChicken.jpg"],nil];





       if (indexPath.row==0) {
         cell.imageView.image= [UIImage imageNamed:@"Res Images
    /chicken grav.jpg"];
    }
     else if (indexPath.row==1) {
        cell.imageView.image= [UIImage imageNamed:@"Res Images
     /crispy chicken.jpg"];
    }
    else  if (indexPath.row==2) {
        cell.imageView.image= [UIImage imageNamed:@"Res Images
        /Buttermilk Chicken.jpg"];
    }
    else if (indexPath.row==3) {
        cell.imageView.image= [UIImage imageNamed:@"Res Images 
             /Southern Chicke.jpg"];
                }
  else  if (indexPath.row==4) {
        cell.imageView.image= [UIImage imageNamed:@"Res Images 
         /Simple Chick.jpg"];


        }}




 if (indexPath.section==1) {

    allindgre=[recipes objectForKey:@"ricerecipes"];
    preparetime=[recipes objectForKey:@"ricepreparetimeing"];
    makingarray=[recipes objectForKey:@"ricemakingsample"];

imagearray=[[NSMutableArray alloc]initWithObjects:[UIImage 
    imageNamed:@"Tofu Fried.jpg"],[UIImage imageNamed:@"chicken fried
       rice.jpg"],[UIImage imageNamed:@"sweet-soy.jpg"],
                [UIImage imageNamed:@"Southeast Asian.jpg"],
    [UIImage imageNamed:@"Spicy Vegetable.jpg"],nil];




     if (indexPath.section==2)
     {

    allindgre=[recipes objectForKey:@"burgerrecipes"];
    preparetime=[recipes objectForKey:@"bugerpreparetimeing"];
    makingarray=[recipes objectForKey:@"bmakingrecipes"];




          imagearray=[[NSMutableArray alloc]initWithObjects:[UIImage 
     imageNamed:@"burger/blue burger.jpg"],[UIImage
    imageNamed:@"burger/serrano pepper.jpg"],[UIImage 
     imageNamed:@"burger/smak bloody mary.jpg"],
                [UIImage imageNamed:@"burger/italian style
         meatball.jpg"],
                [UIImage imageNamed:@"burger/Gold Nugget 
        Burger.jpg"],nil];

    if (indexPath.row==0) {
        UIImage *omeltte=[UIImage imageNamed:@"Images/buger.png"];
        cell.imageView.image = omeltte;


       }
      }




 if (indexPath.section==3)




{


     allindgre=[recipes objectForKey:@"pizzarecipes"];
    preparetime=[recipes objectForKey:@"pizzapreparetimeing"];
    makingarray=[recipes objectForKey:@"pmakingrecipes"];

    imagearray=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"burger/Garlic Chicken Pizza.jpg"],[UIImage imageNamed:@"burger/Pizza On The Grillr.jpg"],[UIImage imageNamed:@"burger/BBQ Chicken Pizza.jpg"],
                [UIImage imageNamed:@"burger/Veggie Pizza.jpg"],
                [UIImage imageNamed:@"burger/Four Cheese Margherita Pizza.jpg"],nil];



    if (indexPath.row==0) {


        UIImage *pizza=[UIImage imageNamed:@"Images/pizza.png"];

        cell.imageView.image = pizza;
    }

}

[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];


[[cell textLabel]setTextColor:[UIColor blackColor]];

UIImage *background = [self cellBackgroundForRowAtIndexPath  :indexPath];

UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;



return cell;


     }




      #pragma mark - gesture tapped
    - (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0  inSection:gestureRecognizer.view.tag];


    if (indexPath.row==0) {

    BOOL collapsed  = [[boolenarry objectAtIndex:indexPath.section]  boolValue];
    collapsed       = !collapsed;
    [boolenarry replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:collapsed]];

}

                //reload specific section animated
   NSRange range   = NSMakeRange(indexPath.section,1);
    NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
    [self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];



     }

          #pragma mark - Segues





   - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:
     (id)sender {



   if ([[segue identifier] isEqualToString:@"showDetail"]) {

        NSIndexPath *indexPath = [self.tableView 
         indexPathForSelectedRow];


    DetailViewController *destViewController=
     segue.destinationViewController;


       destViewController.detailItem = [[recipes objectForKey:
        [Titlearray objectAtIndex:indexPath.section ]] 
 objectAtIndex:indexPath.row];



    NSString*allrec= [allindgre objectAtIndex:indexPath.row];
    [[segue destinationViewController]setPtext:allrec];



     NSString*preparetim =[preparetime objectAtIndex:indexPath.row];

    [[segue destinationViewController] setPretime:preparetim];


    NSString*make=[makingarray objectAtIndex:indexPath.row];

    [[segue destinationViewController]setMethetext:make];



    UIImage*imagestring=[imagearray objectAtIndex:indexPath.row];

    [[segue destinationViewController] setImagess:imagestring];
}}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

我不知道,你在为此尝试什么...但尝试跟随,我希望它可以帮到你:

假设您显示日期和标题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath{
        if (indexPath.section == 0){
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Category =[cd] %@", @"Category 1"];
            NSArray * catArray = [self.notes filteredArrayUsingPredicate:predicate];
            cell.textLabel.text = [[catArray objectAtIndex:indexPath.row] objectForKey:@"Title"];
            cell.detailTextLabel.text = [[catArray objectAtIndex:indexPath.row] objectForKey:@"Date"];

        } else if (indexPath.section == 1){
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Category =[cd] %@", @"Category 1"];
            NSArray * catArray = [self.notes filteredArrayUsingPredicate:predicate];
            cell.textLabel.text = [[catArray objectAtIndex:indexPath.row] objectForKey:@"Title"];
            cell.detailTextLabel.text = [[catArray objectAtIndex:indexPath.row] objectForKey:@"Date"];
        } 
    }

这样做 。

相关问题