IOS控件-tableview

来源:互联网 发布:java sql 批处理 编辑:程序博客网 时间:2024/06/07 15:16

xib中view与file·s· owner关联

xib中tableview的datasource和tableview关联

xib中tableview的delegate和tableview关联

上代码

.h文件

#import <UIKit/UIKit.h>@interface EXTVV2ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>@end


.m文件

////  EXTVV2ViewController.m//  ExerciseTableViewV2////  Created by hxl on 13-5-20.//  Copyright (c) 2013年 xiaolei.hu. All rights reserved.///* UITableView Tasks  //初始化UITableView对象 Initializing a UITableView Object  – initWithFrame:style: - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style   //配置UITableView Configuring a Table View  //tableView的style //UITableViewStylePlain或者UITableViewStyleGrouped,2者选1 style  property @property(nonatomic, readonly) UITableViewStyle style  //当前section有多少行(此方法必须实现) – numberOfRowsInSection: - (NSInteger)numberOfRowsInSection:(NSInteger)section //当前section的标示   //当前tableView里有多少section,默认为1 – numberOfSections - (NSInteger)numberOfSections   //行高 rowHeight  property   separatorStyle  property separatorColor  property //tableview的背景 backgroundView  property    //创建cell Creating Table View Cells – registerNib:forCellReuseIdentifier: – registerClass:forCellReuseIdentifier: – dequeueReusableCellWithIdentifier:forIndexPath: – dequeueReusableCellWithIdentifier:     Accessing Header and Footer Views – registerNib:forHeaderFooterViewReuseIdentifier: – registerClass:forHeaderFooterViewReuseIdentifier: – dequeueReusableHeaderFooterViewWithIdentifier: tableHeaderView  property tableFooterView  property sectionHeaderHeight  property sectionFooterHeight  property – headerViewForSection: – footerViewForSection: Accessing Cells and Sections – cellForRowAtIndexPath: – indexPathForCell: – indexPathForRowAtPoint: – indexPathsForRowsInRect: – visibleCells – indexPathsForVisibleRows Scrolling the Table View – scrollToRowAtIndexPath:atScrollPosition:animated: – scrollToNearestSelectedRowAtScrollPosition:animated: Managing Selections – indexPathForSelectedRow – indexPathsForSelectedRows – selectRowAtIndexPath:animated:scrollPosition: – deselectRowAtIndexPath:animated: allowsSelection  property allowsMultipleSelection  property allowsSelectionDuringEditing  property allowsMultipleSelectionDuringEditing  property Inserting, Deleting, and Moving Rows and Sections – beginUpdates – endUpdates – insertRowsAtIndexPaths:withRowAnimation: – deleteRowsAtIndexPaths:withRowAnimation: – moveRowAtIndexPath:toIndexPath: – insertSections:withRowAnimation: – deleteSections:withRowAnimation: – moveSection:toSection: Managing the Editing of Table Cells editing  property – setEditing:animated: Reloading the Table View – reloadData – reloadRowsAtIndexPaths:withRowAnimation: – reloadSections:withRowAnimation: – reloadSectionIndexTitles Accessing Drawing Areas of the Table View – rectForSection: – rectForRowAtIndexPath: – rectForFooterInSection: – rectForHeaderInSection: Managing the Delegate and the Data Source dataSource  property delegate  property Configuring the Table Index sectionIndexMinimumDisplayRowCount  property sectionIndexColor  property sectionIndexTrackingBackgroundColor  property  */#import "EXTVV2ViewController.h"@interface EXTVV2ViewController ()@property (nonatomic) NSMutableArray *listData;@property (nonatomic) IBOutlet UITableView* myTableView;//在xib中与tableview控件关联@property (nonatomic) IBOutlet UISwitch* mySwitch;//在xib中与switch控件关联-(IBAction)switchEditModel:(UISwitch*)sender;//在xib中与switch控件的事件关联@end@implementation EXTVV2ViewController@synthesize listData;@synthesize myTableView;@synthesize mySwitch;- (void)viewDidLoad{    [superviewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    [selfsetListData:[selfcreateData:26sectionRowLength:10stringLength:6]];}- (void)didReceiveMemoryWarning{    [superdidReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}//指定有多少个分区(Section),默认为1/* 1.此处根据二维数组外层的count获取section数量 此时已有count个section被创建 */- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return [self.listDatacount];}//指定各个分区中有多少行,默认为1。/* 2.此处根据1设置的section数量获取数组二维内层长度(row数量)ps:section会根据你设置的最大值自动递增 此时section对应的count个cell被创建 */- (NSInteger) tableView: (UITableView *) tableView  numberOfRowsInSection: (NSInteger) section {    NSInteger rowCount = 0;    //NSLog(@"section = %d",section);0/1/2    if (section < self.listData.count) {        rowCount = [self.listData[section]count];    }    return rowCount;}//设置每行调用的cell/* 3.此处根据1设置的section数量,和2设置的row数量获取数组内容并填充cell 对1、2创建的容器进行填充,section和row就是二维数组的下标 */- (UITableViewCell *) tableView: (UITableView *) tableView          cellForRowAtIndexPath: (NSIndexPath *) indexPath{    /*     indexPath 索引路径     property:     row:table view 中     item:collection view中     section:table/collection view中     method     //collection view中     + (NSIndexPath *)indexPathForItem:(NSInteger)item inSection:(NSInteger)section     //table view 中     + (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section          */        //产生一个静态标示(每个cell形式相同可用)    //static NSString * TableSampleIdentifier = @ "TableSampleIdentifier";    //每个cell形式不相同需要不同标示    NSString * TableSampleIdentifier = [[NSStringalloc]initWithFormat:@"CMainCell%d", indexPath.row];        //通过标示符获取一个cell对象(dequeueReusableCellWithIdentifier=>系统请求的回调函数)    UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:                              TableSampleIdentifier];    //如果未获取到cell对象,创建新的cell对象,并赋予标示符    if (cell == nil) {        cell = [[UITableViewCellalloc]                initWithStyle:UITableViewCellStyleDefault                reuseIdentifier: TableSampleIdentifier];    }    NSString* cellText = nil;    if (indexPath.section <self.listData.count) {        NSArray* rowArray = self.listData[indexPath.section];        if ([indexPath row] < rowArray.count) {            cellText = rowArray[indexPath.row];        }    }    cell.textLabel.text = cellText;    return cell;}//设置每个section显示的Title- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    NSString* title = nil;    if (section < self.listData.count) {        NSArray* rowArray = self.listData[section];        if (rowArray.count >0) {            //将每个section的第一行作为title是惯例            title = rowArray[0];        }    }    //截取首字母    return [titlesubstringToIndex:1];}//设置tableview每行的title(右侧索引)- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    //initWithCapacity初始化数组时候指定长度    NSMutableArray* indexTitleArray = [[NSMutableArrayalloc]initWithCapacity:[self.listDatacount]];    //循环外围数组(section个数)    for (UInt16 i =0; i < [self.listDatacount]; i++) {        NSArray* rowArray = self.listData[i];        //判断section下的数据行是否大于0        if (rowArray.count >0) {            NSString* titleStr = rowArray[0];            //title长度超过3截取字符串            if (titleStr.length >1) {                titleStr = [titleStr substringToIndex:1];            }            [indexTitleArray addObject:titleStr];        }    }    //arrayWithArray产生一个新数组并释放原来的数组    return [NSArrayarrayWithArray:indexTitleArray];}//点击右侧索引时响应跳转到那个section的事件- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{    return index;}//设置选中Cell的响应事件/* -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失 } *///选中之前执行-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{    return indexPath;}//设置划动cell是否出现del按钮-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}//设置删除时编辑状态-(void)tableView:(UITableView *)tableView  commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    //删除元素的操作    if (editingStyle ==UITableViewCellEditingStyleDelete)    {        //删除数据        [self.listData[indexPath.section]removeObjectAtIndex:indexPath.row];        //删除元素        [tableView deleteRowsAtIndexPaths:[NSMutableArrayarrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationTop];            }}//选中cell后触发的事件-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{        //设置选中的样式,4种风格 UITableViewCellAccessoryCheckmark UITableViewCellAccessoryDetailDisclosureButton    //UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryNone    UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];    if (cellView.accessoryType ==UITableViewCellAccessoryNone) {        cellView.accessoryType=UITableViewCellAccessoryCheckmark;    }    else {        cellView.accessoryType =UITableViewCellAccessoryNone;        [tableView deselectRowAtIndexPath:indexPathanimated:YES];    }        //弹出框    NSString *cellSelected=[self.listData[indexPath.section]objectAtIndex:indexPath.row];    //indexPath.row得到选中的行号,提取出在数组中的内容。    UIAlertView *myAlertView;    myAlertView = [[UIAlertViewalloc]initWithTitle:@"你选中了:" message:cellSelected delegate:selfcancelButtonTitle:@"ok"otherButtonTitles:nil];    //点击后弹出该对话框。    [myAlertView show];    }//是否能移动- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {    return YES;}//移动操作- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    if (sourceIndexPath != destinationIndexPath) {        id object = [self.listData[sourceIndexPath.section]objectAtIndex:sourceIndexPath.row];        [self.listData[sourceIndexPath.section]removeObjectAtIndex:sourceIndexPath.row];        if (destinationIndexPath.row > [self.listData[destinationIndexPath.section]count]) {            [self.listData[destinationIndexPath.section]addObject:object];        }        else {            [self.listData[destinationIndexPath.section]insertObject:objectatIndex:destinationIndexPath.row];        }    }}//单元格返回的编辑风格,包括删除 添加和默认  和不可编辑三种风格//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath//{//return UITableViewCellEditingStyleDelete;//return UITableViewCellEditingStyleNone;//return UITableViewCellEditingStyleInsert;//}//switc按钮事件-(IBAction)switchEditModel:(UISwitch*)sender{    //self.view.subview所有子视图,包括tableview等    //是否开启编辑模式    if(sender.on) {        [self.myTableViewsetEditing:YESanimated:YES];    } else {        [self.myTableViewsetEditing:NOanimated:YES];    }    }//生成随机字符串- (NSString *) createRandString:(NSInteger)stringLength perStr:(UInt16)pstr{    UInt16 seed = 0;    //97-122小写英语    NSMutableString *str = [[NSMutableStringalloc]initWithFormat:@"%c", pstr];    for(UInt16 i = 0; i < stringLength; i++) {        seed = (arc4random() % 26) + 97;        [str appendFormat:@"%c", seed];    }    return [NSStringstringWithString:str];}- (NSMutableArray *) createData:(NSInteger)sectionLength sectionRowLength:(NSInteger)row stringLength:(NSInteger)length{    NSMutableArray *sectionData = [[NSMutableArrayalloc]initWithCapacity:sectionLength];    for (UInt16 i =0; i < sectionLength ; i++) {        NSMutableArray* rowData = [[NSMutableArrayalloc]initWithCapacity:row];        for (UInt16 j =0; j < row; j++) {            [rowData addObject:[selfcreateRandString:lengthperStr:i +97]];        }        [sectionData addObject:rowData];    }    return sectionData;}@end

原创粉丝点击