繁体   English   中英

UITableView-分组表。 iOS(可可触控),Objective-C

[英]UITableView - Grouped Table. iOS (Cocoa Touch), Objective-C

我找到了UITableView的教程-深入研究表格视图, 源代码 ,但我想添加一些升级。 我的目标是在级别0(CurrentLevel == 0)中添加具有2个部分的分组表视图。 所以我开始:

  1. 将表视图样式(在RootViewController.xib)从“普通”更改为“分组”。 (工作正常)。

  2. RootViewController.m的更改:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(CurrentLevel == 0)    
    return 2;
else
    return 1;
  1. 将新密钥添加到名为:Other的data.plist中。

之后,CurrentLevel 0将为objectForKey:@“ Rows”返回具有相同数据的2个部分。 我不知道改变有多热

-(void)viewDidLoad, 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section,
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

从data.plist中为我的新键@“ Other”加载数据的方法,其中键@“ Other”表示CurrentLevel 0表视图中表的第二部分。

这是以下代码的源代码:

DrillDownAppAppDelegate.h

//
//  DrillDownAppAppDelegate.h
//  DrillDownApp
//
//  Created by iPhone SDK Articles on 3/8/09.
//  Copyright www.iPhoneSDKArticles.com 2009. 
//

#import <UIKit/UIKit.h>

@interface DrillDownAppAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;

    NSDictionary *data;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property (nonatomic, retain) NSDictionary *data;

@end

DrillDownAppAppDelegate.m

//
//  DrillDownAppAppDelegate.m
//  DrillDownApp
//
//  Created by iPhone SDK Articles on 3/8/09.
//  Copyright www.iPhoneSDKArticles.com 2009. 
//

#import "DrillDownAppAppDelegate.h"
#import "RootViewController.h"


@implementation DrillDownAppAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize data;


- (void)applicationDidFinishLaunching:(UIApplication *)application {

    NSString *Path = [[NSBundle mainBundle] bundlePath];
    NSString *DataPath = [Path stringByAppendingPathComponent:@"data.plist"];

    NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
    self.data = tempDict;
    [tempDict release];

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}


- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}  


- (void)dealloc {
    [data release];
    [navigationController release];
    [window release];
    [super dealloc];
}

@end

RootViewController.h

//
//  RootViewController.h
//  DrillDownApp
//
//  Created by iPhone SDK Articles on 3/8/09.
//  Copyright www.iPhoneSDKArticles.com 2009. 
//

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {

NSArray *tableDataSource;
NSString *CurrentTitle;
NSInteger CurrentLevel;
}

@property (nonatomic, retain) NSArray *tableDataSource;
@property (nonatomic, retain) NSString *CurrentTitle;
@property (nonatomic, readwrite) NSInteger CurrentLevel;

@end

RootViewController.m

//
//  RootViewController.m
//  DrillDownApp
//
//  Created by iPhone SDK Articles on 3/8/09.
//  Copyright www.iPhoneSDKArticles.com 2009. 
//

#import "RootViewController.h"
#import "DrillDownAppAppDelegate.h"
#import "DetailViewController.h"

@implementation RootViewController

@synthesize tableDataSource, CurrentTitle, CurrentLevel;


- (void)viewDidLoad {
    [super viewDidLoad];

    if(CurrentLevel == 0) {

        //Initialize our table data source
        NSArray *tempArray = [[NSArray alloc] init];
        self.tableDataSource = tempArray;
        [tempArray release];

        DrillDownAppAppDelegate *AppDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
        self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];

        self.navigationItem.title = @"Zvolte trasu";
    }
    else 
        self.navigationItem.title = CurrentTitle;   
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(CurrentLevel == 0)    
        return 2;
    else
        return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.tableDataSource count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...


    NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
    cell.textLabel.text = [dictionary objectForKey:@"Title"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    UIImage *rowImage = [UIImage imageNamed:[dictionary objectForKey:@"Image"]];
    cell.imageView.image = rowImage;

    return cell;
}


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

    //Get the dictionary of the selected data source.
    NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];

    //Get the children of the present item.
    NSArray *Children = [dictionary objectForKey:@"Children"];

    if([Children count] == 0) {

        DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:dvController animated:YES];
        [dvController release];
    }
    else {

        //Prepare to tableview.
        RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];

        //Increment the Current View
        rvController.CurrentLevel += 1;

        //Set the title;
        rvController.CurrentTitle = [dictionary objectForKey:@"Title"];

        //Push the new table view on the stack
        [self.navigationController pushViewController:rvController animated:YES];

        rvController.tableDataSource = Children;

        [rvController release];
    }
}

- (void)dealloc {
    [CurrentTitle release];
    [tableDataSource release];
    [super dealloc];
}

@end

编辑:

我的尝试:

RootViewController.h

//
//  RootViewController.h
//  DrillDownApp
//
//  Created by iPhone SDK Articles on 3/8/09.
//  Copyright www.iPhoneSDKArticles.com 2009. 
//

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {

NSArray *tableDataSource;
    NSArray *otherDataSource;
NSString *CurrentTitle;
NSInteger CurrentLevel;
}

@property (nonatomic, retain) NSArray *tableDataSource;
@property (nonatomic, retain) NSArray *otherDataSource;
@property (nonatomic, retain) NSString *CurrentTitle;
@property (nonatomic, readwrite) NSInteger CurrentLevel;

@end

RootViewController.m

//
//  RootViewController.m
//  DrillDownApp
//
//  Created by iPhone SDK Articles on 3/8/09.
//  Copyright www.iPhoneSDKArticles.com 2009. 
//

#import "RootViewController.h"
#import "DrillDownAppAppDelegate.h"
#import "DetailViewController.h"

@implementation RootViewController

@synthesize tableDataSource, otherDataSource, CurrentTitle, CurrentLevel;


- (void)viewDidLoad {
    [super viewDidLoad];

    if(CurrentLevel == 0) {

        //Initialize our table data source
    NSArray *tempArray = [[NSArray alloc] init];
    self.tableDataSource = tempArray;
    self.otherDataSource = tempArray;
            [tempArray release];

    DrillDownAppAppDelegate *AppDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
    self.otherDataSource = [AppDelegate.data objectForKey:@"Other"];

    self.navigationItem.title = @"Root";
}
else 
    self.navigationItem.title = CurrentTitle;   
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(CurrentLevel == 0)
        return 2;
    else
        return 1;
}


// Customize the number of rows in the table view.
//Not sure about this. When I select some cell from section 1, what will appear at didSelectRowAtIndexPath?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0 && CurrentLevel == 0)
        return [self.tableDataSource count];
    if(section == 1 && CurrentLevel == 0)
        return [self.tableDataSource count];
    else 
        return [self.tableDataSource count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if(section == 0 && CurrentLevel == 0)
        return nil;
    else
        return nil;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell... don't know how.

    //here is only tableDataSource
    NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.section];
    cell.textLabel.text = [dictionary objectForKey:@"Title"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    UIImage *rowImage = [UIImage imageNamed:[dictionary objectForKey:@"Image"]];
    cell.imageView.image = rowImage;

    return cell;
}

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

    //only tableDataSource
    NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];

    //Get the children of the present item.
    NSArray *Children = [dictionary objectForKey:@"Children"];

    if([Children count] == 0) {

        DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];

        [self.navigationController pushViewController:dvController animated:YES];
        [dvController release];
    }
    else {

        //Prepare to tableview.
        RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];

        //Increment the Current View
        rvController.CurrentLevel += 1;

        //Set the title;
        rvController.CurrentTitle = [dictionary objectForKey:@"Title"];

        //Push the new table view on the stack
        [self.navigationController pushViewController:rvController animated:YES];

        rvController.tableDataSource = Children;

        [rvController release];
    }
}

- (void)dealloc {
    [CurrentTitle release];
    [tableDataSource release];
    [otherDataSource release];
    [super dealloc];
}

@end
  • 创建第二个数组来保存第二部分的数据
  • 使用您的@“ Other”键在viewDidLoad填充此数组
  • 在相关的数据源方法中使用indexPath.section ,以使您知道表视图询问您的哪个部分,并返回相关数组的对象/计数。 indexPath.section对于第一部分将为0,对于第二部分将为1。
  • 无论在何处使用tableDataSource ,都需要检查您所在的节。它可以作为整数(节)或作为索引路径的一部分(因此可以通过indexPath.section获取)传递。 如果您在第0节中,请使用tableDataSource,否则,如果在第1节中,请使用otherDataSource。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM