iPhone UITableView Sections

前端 未结 2 1678
一个人的身影
一个人的身影 2020-12-06 02:34

I have been programming a UITableView and each cells pushes a new view, All I Want to do is add two new sections one male and one female, first and second voice need to be i

相关标签:
2条回答
  • 2020-12-06 03:19

    The tableView delegate has a method called numberOfSectionsInTableView. Return the number of sections that you want to create.

    Then, in the cellForRowAtIndexPath use indexPath's other property [indexPath section] to segregate rows based on sections.

    An example

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 2; //one male and other female
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    {
        switch(section){
           case 0:
           return [male count];
           break;
           case 1:
           return [female count];
           break;
        }
    }
    
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     {
    
        static NSString *FirstLevelCell= @"FirstLevelCell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
        }
         SecondLevelViewController *controller;
        switch([indexPath section]){
           case 0:
           controller = [male objectAtIndex: [indexPath row] ];
           break;
           case 1:
           controller = [female objectAtIndex: [indexPath row] ];
           break;
        }
        cell.textLabel.text = controller.title;
        cell.imageView.image = controller.rowImage;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
    0 讨论(0)
  • 2020-12-06 03:37

    Hows this?

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // Return the number of sections.
        return 2;
    }
    
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        switch (section) {
            case 0:
                return # of rows in section;
                break;
            case 1:
                return # of rows in section;
    break;
        }
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题