iPhone UITableView部分 [英] iPhone UITableView Sections

查看:85
本文介绍了iPhone UITableView部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编程一个UITableView,每个单元格推新的视图,所有我想做的是添加两个新部分一个男性和一个女性,第一和第二个声音需要在男性部分,第三个声音需要

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 in the male section and the third voice needs to be in the female section.

#import "FirstLevelViewController.h"
#import "SecondLevelViewController.h"
#import "DisclosureDetailController.h"
#import "SecondVoiceController.h"
#import "ThirdVoiceController.h"



@implementation FirstLevelViewController
@synthesize controllers;

-(void)viewDidLoad {
    self.title = @"Voices";

    NSMutableArray *male = [[NSMutableArray alloc] init];


    DisclosureDetailController *th = [DisclosureDetailController alloc];
    th.title = @"First Voice";
    [male addObject:th];
    [th release];

    SecondVoiceController *array2 = [SecondVoiceController alloc];
    array2.title = @"Second Voice";
    [male addObject:array2];
    [array2 release];

    ThirdVoiceController *array3 = [ThirdVoiceController alloc];
    array3.title = @"Third Voice";
    [male addObject:array3];
    [array3 release];




    self.controllers = male;
    [male release];
    [super viewDidLoad];
}

-(void)viewDidUnload {

    self.controllers = nil;
    [super viewDidUnload];
}

-(void)dealloc {

    [controllers release];
    [super dealloc];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.controllers count];
}

-(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];
    }
    NSUInteger row = [indexPath row];
    SecondLevelViewController *controller = [controllers objectAtIndex:row];
    cell.textLabel.text = controller.title;
    cell.imageView.image = controller.rowImage;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];

    SecondLevelViewController *nextViewController = [self.controllers
                                                     objectAtIndex:row];

    [self.navigationController pushViewController:nextViewController animated:YES];
}



所有我想做的是添加两个新部分一个男性和一个女性,第一和第二语音需要在男性部分中,并且第三语音需要在女性部分中。

All I Want to do is add two new sections one male and one female, first and second voice need to be in the male section and the third voice needs to be in the female section. Please help been stuck on this for a while!

推荐答案

tableView委托有一个名为 numberOfSectionsInTableView 。返回您要创建的节数。

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

然后,在cellForRowAtIndexPath中使用 indexPath 的其他属性 [indexPath section] 以根据节分隔行。

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

示例

- (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;
}

这篇关于iPhone UITableView部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆