代码之家  ›  专栏  ›  技术社区  ›  Isuru PapaSmurf

填充分组的UITableView

  •  1
  • Isuru PapaSmurf  · 技术社区  · 12 年前

    我正在尝试填充分组 UITableView 使用 NSMutableArray 。我希望数组中的每个元素都有自己的部分。即:每个部分一个元素(一行)。

    这是我迄今为止编写的代码。

    #import "MailViewController.h"
    
    @interface MailViewController ()
    
    @end
    
    @implementation MailViewController
    
    NSMutableArray *mailboxes;
    
    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
    
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        mailboxes = [[NSMutableArray alloc] initWithObjects:@"Inbox", @"Drafts", @"Sent Items", nil];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return mailboxes.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 1;
    }
    
    - (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];
        }
        cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
    
        return cell;
    }
    
    @end
    

    目前情况就是这样。

    enter image description here

    我怎样才能按照上面描述的方式得到这个?(第一部分:收件箱,第二部分:草稿,第三部分:已发送邮件等)我已经接近了,但还没有完全到达。

    非常感谢。

    4 回复  |  直到 12 年前
        1
  •  5
  •   iDev    12 年前

    您应该更改:

    cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
    

    cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
    

    这些行应该在 cellForRowAtIndexPath: 方法

    数组索引应该基于节而不是行。行总是固定在零。

        2
  •  1
  •   Jason Silberman    12 年前

    代替

        cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
    

    具有

        cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
    
        3
  •  0
  •   Matej    12 年前

    如果您也想要小节标题,请实现 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 并返回一个基于节的字符串。。

    使用其他答案中的代码,您将能够在每个部分中显示一个单元格。我更喜欢做的是多维度 NSArray 预先构建的细胞,当我需要它们时,它们就在索引中 [section][row] .

    您目前有一个只有部分的数组,并试图将它们作为行访问(正如其他答案所建议的那样)。如果在一个表组中有多个邮箱,则代码将不起作用。。

        4
  •  -1
  •   alok srivastava    12 年前
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     {
        return [mailboxes count];
     }
    
       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
      {
       return 1;
      }
    
      - (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];
        }
          switch(indexpath.section){
                 case 0:
                  cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
                  case 1:
                   cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
                   case 2:
                   cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
                    default:
                     break;
                 }
       return cell;
     }
    
    推荐文章