IOS-UITabView示例 – iPhone手機開發技術文章 iPhone軟體開發教學課程

總結

本章主要簡示瞭UITabView得使用方法,使用UITabViewController的三個頁面演示不同情況,包括簡單的表格,分段帶副標題風格的表格以及索引表格。

運行結果

工程構建概要

1.UITabViewController的使用方法可參考以前的文章vcD4KPHA+PC9wPgo8cCBjbGFzcz0=”p1″>2.使用UITabView的控制類必須實現該控件對應的數據源和代理的方法,同時需要在IB裡面指定UITabView的數據源和代理為該界面的控制類。

3.主要實現的方法有有幾個段,每個段的行數,每個單元格怎麼繪制,段標題是什麼,如果需要索引的話,還得實現索引標題,點擊索引標題定位到段。

4.因為本工程沒有使用ARC,所以一開始有內存管理的問題,主要是成員變量賦值的時候是淺拷貝。正確的使用方法是self._value = value,而不是_vale = value,前者會調用設置方法,而後者隻是指針賦值。

主要代碼及註釋

h文件

//
//  IndexViewController.h
//  TableViewDemo
//
//  Created by arbboter on 14/12/5.
//  Copyright (c) 2014年 arbboter. All rights reserved.
//

#import 

@interface IndexViewController : UIViewController 
{
    NSMutableArray* _arraySection;
    NSMutableArray* _arrayMember;
    NSArray* _arrayIndex;
}

@property (nonatomic, retain) NSMutableArray* _arraySection;
@property (nonatomic, retain) NSMutableArray* _arrayMember;
@property (nonatomic, retain) NSArray* _arrayIndex;
@end

m文件

//
//  IndexViewController.m
//  TableViewDemo
//
//  Created by arbboter on 14/12/5.
//  Copyright (c) 2014年 arbboter. All rights reserved.
//

#import "IndexViewController.h"

@implementation IndexViewController

@synthesize _arrayMember;
@synthesize _arraySection;
@synthesize _arrayIndex;


#pragma UITableViewDataSource
// 對應段的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger ret = 0;
    ret = [[_arraySection objectAtIndex:section] count];
    return ret;
}

// 繪制單元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = nil;
    static NSString *CellIdentifier = @"Cell1";
    
    cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:CellIdentifier] autorelease];
    }
    
    cell.textLabel.text = [[_arraySection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [_arrayIndex objectAtIndex:indexPath.section]]];
    return cell;
}

// 索引標題列表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray* index  = [[NSMutableArray alloc] init];
    
    for (int i='0'; i<='9'; i++)
    {
        [index addObject:[NSString stringWithFormat:@"%c", i]];
    }
    
    for (int i='A'; i<='Z'; i++)
    {
        [index addObject:[NSString stringWithFormat:@"%c", i]];
    }
    
    self._arrayIndex = index;
    [index release];
    return _arrayIndex;
}

// 點擊索引標題定位到段
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}
// 分段數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return [_arraySection count];
}

// 返回段標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_arrayIndex objectAtIndex:section];
}

#pragma UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* strRow = nil;
    
    NSArray* array = [_arraySection objectAtIndex:indexPath.section];
    strRow = [array objectAtIndex:indexPath.row];
    
    NSString* msg = [[NSString alloc] initWithFormat:@"你選擇瞭-%@", strRow];
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    
    [alert show];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [msg release];
    [alert release];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSMutableArray* arrayIndex = [[NSMutableArray alloc] init];
    
    // 數字名
    for (char i='0'; i<='9'; i++)
    {
        int nMem = arc4random()%7+4;
        NSMutableArray* arrayMem = [[NSMutableArray alloc] init];
        for (int j=0; j<nMem; j++)
        {
            [arrayMem addObject:[NSString stringWithFormat:@"%c%c%c", i, arc4random()%10+'0', arc4random()%10+'0']];
        }
        [arrayIndex addObject:arrayMem];
    }
    
    // 字母名
    for (char i='A'; i<='Z'; i++)
    {
        int nMem = arc4random()%7+4;
        NSMutableArray* arrayMem = [[NSMutableArray alloc] init];
        for (int j=0; j<nMem; j++)
        {
            [arrayMem addObject:[NSString stringWithFormat:@"%c%c%c", i, arc4random()%26+'a', arc4random()%26+'a']];
        }
        [arrayIndex addObject:arrayMem];
    }
    
    
    
    self._arraySection = arrayIndex;
    self._arrayMember = [arrayIndex objectAtIndex:0];
    [arrayIndex release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    for (int i=0; i<_arraySection.count; i++)
    {
        [[_arraySection objectAtIndex:i] release];
    }
    [_arraySection release];
    [_arrayMember release];
    [super dealloc];
}

@end

工程代碼下載

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *