- (void)viewDidLoad {

[super viewDidLoad];

tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.height)style:UITableViewStyleGrouped];

//    UITableViewStylePlain,

//    UITableViewStyleGrouped

tableview.delegate =self;//不要忘写了这两句话哟调用delegate*/

tableview.dataSource=self;

/*改变 UITableViewStyleGrouped 样式的 背景色 */

tableview.backgroundView = [[UIView alloc]init];

tableview.backgroundColor = [UIColor clearColor];

[self.view addSubview:tableview];

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

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

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

for (int i = 1; i<= 5; i++)

{

NSString *value = [NSString stringWithFormat:@"%d",i];

NSString *imageName = [NSString stringWithFormat:@"image%@.png",value];

UIImage *image = [UIImage imageNamed:imageName];

//        NSLog(@"imageName == %@",imageName);

[arrayValue addObject:value];

[arrayImageValue addObject:image];

}

for (int i = 6;i<=10; i++ )

{

NSString *value = [NSString stringWithFormat:@"%d",i];

NSString *imageName = [NSString stringWithFormat:@"image%@.png",value];

UIImage *image = [UIImage imageNamed:imageName];

[arrayImageValue2 addObject:image];

}

array1 = arrayValue;

arrayImage = arrayImageValue;

arrayImage1 =arrayImageValue2;

/*

//添加一个BarButton用来控制cell的移动

UIBarButtonItem *moveButton = [[UIBarButtonItem alloc]

initWithTitle:@"移动"

style:UIBarButtonItemStylePlain

target:self

action:@selector(toggleMove)];

self.navigationItem.rightBarButtonItem = moveButton;

//默认设置cell不可编辑

[tableview setEditing:tableview.editing=NO];

*/

}

/**

*explain:事件响应toggleMove

*/

//-(void)toggleMove

//{

//    //设置cell编辑模式取反

//    [tableview setEditing:!tableview.editing];

//

//    //初始话时默认不可编辑 点击事件的时候取反 为真!可编辑

//

//    if (self.tableview.editing)

//    {

//        [self.navigationItem.rightBarButtonItem setTitle:@"完成"];

//    }else

//    {

//        [self.navigationItem.rightBarButtonItem setTitle:@"移动"];

//    }

//

//}

/**

*explain:设置cell是否可以移动(滑动)

*/

//- (BOOL)tableView:(UITableView *)tableView

//canMoveRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    return YES; //可以移动

//}

///**

// *explain:设置cell的插入和删除位置

// */

//- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath   toIndexPath:(NSIndexPath *)toIndexPath {

//    NSUInteger fromRow = [fromIndexPath row];  //要移动的那个cell integer

//    NSUInteger toRow = [toIndexPath row]; //要移动位置的那个clell integer

//    //arrayValue 添加数据的那个可变数组

//    id object = [_arrayValue objectAtIndex:fromRow]; // 获取数据

//    [_arrayValue removeObjectAtIndex:fromRow];  //在当前位置删除

//    [_arrayValue insertObject:object atIndex:toRow]; //插入的位置

//

//}

/**

*explain:设置编辑样式:editingStyle(三种)

typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {

UITableViewCellEditingStyleNone, 只有移动

UITableViewCellEditingStyleDelete,滑动显示删除

UITableViewCellEditingStyleInsert,编辑模式和滑动

*/

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

return UITableViewCellEditingStyleDelete;  //滑动出现删除操作,出现的红色按钮(删除这个方法默认是这个模式)

}

/*改变删除按钮的title*/

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

return @"删除";  //默认返回“delete”,这里我们改为中文

}

/*删除cell用到的函数*/

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete)

{

[_arrayValue removeObjectAtIndex:[indexPath row]];  //删除数组里的数据

//删除对应数据的cell,[这里没有做更新数据的操作]

[tableview deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

}

/* 这个函数是显示tableview的章节数*/

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

return 2;

}

/* 这个函数是指定显示多少cells*/

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [array1 count];//这个是指定加载数据的多少即显示多少个cell,如过这个地方弄错了会崩溃的哟

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//定义个静态字符串为了防止与其他类的tableivew重复

static NSString *CellIdentifier =@"Cell";

//定义cell的复用性当处理大量数据时减少内存开销

UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell ==nil)

{

//        cell = [[UITableViewCell alloc]initWithStyle UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

cell.showsReorderControl =YES;  //我们添加一个重新排序控件

if (indexPath.section == 0)

{

cell.imageView.image = [arrayImage objectAtIndex:[indexPath row]];

}

else

{

cell.imageView.image = [arrayImage1 objectAtIndex:[indexPath row]];

}

/*去掉方框 ,或者把view 换成imageview */

UIView *backView = [[UIView alloc]init];

[cell setBackgroundView:backView];

cell.backgroundColor = [UIColor clearColor];

cell.detailTextLabel.text = [array1 objectAtIndex:[indexPath row]];

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

}

// 设置字体大小

cell.textLabel.font = [UIFont systemFontOfSize:15];

/* 问题三 如何改变字体的颜色?

解决方案:

设置字体颜色*/

cell.textLabel.textColor = [UIColor redColor];

/* 问题三 如何改变字体的靠左对齐还是右还是中?

解决方案:

设置字体对齐方式*/

cell.textLabel.textAlignment = NSTextAlignmentCenter;

//设置文字的label的坐标

cell.textLabel.frame = cell.frame;

//设置tag值

//    cell.textLabel.tag =20;

/*  //cell的样式1:右侧添加箭头,表示点击cell 可以pus 到下个viewcontrol

cell.accessoryType =    UITableViewCellAccessoryDisclosureIndicator;

*/

//cell的样式2:右侧添加箭头,和一个info按钮,系统自己定义的button

// 该样式会调用accessoryButtonTappedForRowWithIndexPath方法

cell.accessoryType =  UITableViewCellAccessoryDetailDisclosureButton;

/*

//cell的样式3:这个是选择数据时候 告诉我们选择了哪一行,会调用didSelectRowAtIndexPath方法

cell.accessoryType = UITableViewCellAccessoryCheckmark;

NSUInteger row = [indexPath row];

NSUInteger oldRow = [lastpath row];

//如何点击当前的cell 最右边就会出现一个对号 ,在点击其他的cell 对号显示当前,上一个小时

cell.accessoryType =  (row==oldRow &&lastpath != nil)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;

*/

/*

//cell的样式4:自定义右侧的butten

mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

mybutton.frame = CGRectMake(0, 0, 100, 25);

[mybutton setTitle:@"myButton" forState:UIControlStateNormal];

[mybutton setBackgroundImage:[UIImage imageNamed:@"message"] forState:UIControlStateNormal];

[mybutton addTarget:self action:@selector(myBtnClick:event:) forControlEvents:UIControlEventTouchUpInside];

cell.accessoryView = mybutton;

*/

return cell;

}

/**

*explain:myBtnClick

*/

-(void)myBtnClick:(id)sender event:(id)event

{

NSSet *touches = [event allTouches];   // 把触摸的事件放到集合里

UITouch *touch = [touches anyObject];   //把事件放到触摸的对象了

CGPoint currentTouchPosition = [touch locationInView:self.tableview]; //把触发的这个点转成二位坐标

NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:currentTouchPosition]; //匹配坐标点

if(indexPath !=nil)

{

[self tableView:self.tableview accessoryButtonTappedForRowWithIndexPath:indexPath];

}

}

/**

*explain:添加响应事件

*/

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

{

/*   //这个表示点击选中的那个cell上的数据会弹出警告框

NSString *titileString = [array objectAtIndex:[indexPath row]];

//

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"message:titileString delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil];

[alert show];

*/

//点击选中的那个cell会给该行cell打勾

NSInteger newRow = [indexPath row];

NSInteger oldRow = (lastpath != nil) ? [lastpath row] : -1;

if (newRow != oldRow) {

UITableViewCell *newCell = [tableView cellForRowAtIndexPath:

indexPath];

newCell.accessoryType = UITableViewCellAccessoryCheckmark;

UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:

lastpath];

oldCell.accessoryType = UITableViewCellAccessoryNone;

lastpath = indexPath;

}

// 取消选择(打勾)状态

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

/**

*explain:当cell.accessoryType =  UITableViewCellAccessoryDetailDisclosureButton;时调用

*/

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

NSString *titileString = [NSString stringWithFormat:@"你点击了按钮%@",[array1 objectAtIndex:[indexPath row]]];

UIAlertView *alert = [[ UIAlertView alloc]initWithTitle:@"提示" message:titileString delegate:self    cancelButtonTitle:@"OK"otherButtonTitles: nil];

[alert show];

}

/*设置标题头的宽度*/

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

return 20;

}

/*设置标题尾的宽度*/

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

return 20;

}

/*设置标题头的名称*/

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

{

if (section == 0) {

return @"我是头一";

}

else{

UILabel *label2=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];

label2.text=@"我是程序员";

label2.backgroundColor =[UIColor blueColor];

return label2.text;

}

}

/*设置标题脚的名称*/

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

if (section == 0)

{

return @"我是脚一";

}

else

{

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[button setTitle:@"我是button 2" forState:UIControlStateNormal ];

return button.titleLabel.text;

}

}

/*设置cell 的宽度 */

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 50;

}

/**

*explain:我想把第一个cell显示的数据 向后缩进10个像素其他的cell不变怎么办呢

*/

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

{

//让第一行缩进10个像素,其他的cell不变

if (indexPath.row ==0) {

return 10;

}

return 0;

}

/**

*explain: 屏蔽指定的cell 的触发事件(对自定义按钮无效,针对系统自带),会用到这个函数

*/

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

//我让第一个cell 点击的时候没有反应

if (indexPath.row ==0) {

return nil;

}

return indexPath;

}

/**

*explain:允许Menu菜单(给 tableview 加个长按快捷菜单)

*/

-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath

{

return   YES;

}

//每个cell都可以点击出现Menu菜单

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender

{

//1只显示 copy,不显示其他

/*   if (action == @selector(cut)){

return NO;

}  else if(action == @selector(copy:)){

return YES;

}

else if(action == @selector(paste:)){

return NO;

}

else if(action == @selector(select:)){

return NO;

}

else if(action == @selector(selectAll:)){

return NO;

}    else  {

return [super canPerformAction:action withSender:sender];

}

*/

// 2直接返回yes,表示全部显示

return YES;

}

-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender

{

if (action ==@selector(copy:)) {

//复制到剪切板

NSString *str=[array1 objectAtIndex:[indexPath row]];

[[UIPasteboard generalPasteboard] setPersistent:YES];

[[UIPasteboard generalPasteboard] setValue:str forPasteboardType:[UIPasteboardTypeListString objectAtIndex:0]];

}

if (action ==@selector(cut:)) {

//复制到剪切板

NSString *str=[array1 objectAtIndex:[indexPath row]];

[[UIPasteboard generalPasteboard] setPersistent:YES];

[[UIPasteboard generalPasteboard] setValue:str forPasteboardType:[UIPasteboardTypeListString objectAtIndex:0]];

//剪切完成以后清空

[_arrayValue  replaceObjectAtIndex:indexPath.row withObject:@""];

//刷新cell

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationNone];

}

if (action ==@selector(paste:)) {

NSString *copystr=[array1 objectAtIndex:[indexPath row]];

NSString *pasteString = [UIPasteboard  generalPasteboard].string;

NSLog(@"pasteString:%@",pasteString);

NSString *tmpString = [NSString  stringWithFormat:@"%@%@",copystr,pasteString];

NSLog(@"tmpString:%@",tmpString);

#warning <#message#>

[_arrayValue replaceObjectAtIndex:[indexPath row] withObject:tmpString];

//       [array se]

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationNone];

}

}

/**

*explain:给 tableview 添加索引

*/

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

return array1;

}

UITableView的一些常用设置和代理方法的更多相关文章

  1. UITableView的常用属性和代理方法

    以下是近期总结的关于tableView的一些属性和代理方法,以及一些常见的问题,现汇总如下,今后还会持续更新,请继续关注:   tableView 的头部和尾部视图属性: UISwitch *foot ...

  2. Linux设置全局代理与yum代理

    设置全局代理,方法如下: 修改 /etc/profile 文件,添加下面内容: http_proxy=http://username:password@yourproxy:8080/ ftp_prox ...

  3. UICollectionView的简单使用和常用代理方法

    UICollectionView相对于UITableView有更加自由的布局,做出的界面可变性更大最近开始接触使用UICollectionView,整理了一下常用的代理方法 首先需要先添加UIColl ...

  4. IOS UITableview代理方法总结

    tableview的datasource代理 @required的两个数据源方法 1.返回每个 session 中 cell 的个数 - (NSInteger)tableView:(UITableVi ...

  5. UITableView表格视图、UITableView代理方法及应用

    一.基本知识点 UITableView表格视图,是一个可以滚动的界面(理解为垂直滚动的UIScrollView),可展示多行数据,没有行数的限制,只能有一列. 使用UITableView: 1.展示信 ...

  6. IOS UITableView的代理方法详解

    一.UITableViewDataSourc(数据源代理) 1.必须实现的回调方法 返回每个分区的行数 - (NSInteger)tableView:(UITableView *)tableView ...

  7. [Swift通天遁地]一、超级工具-(8)地图视图MKMapView的常用代理方法

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. UITableView的全部属性、方法以及代理方法执行顺序,看过之后肯定有收获---董鑫

    UITableView-------表视图--继承UIScrollView并遵守NSCoding协议 属性 frame-------------设置控件的位置和大小 backgroundColor-- ...

  9. UIScrollView的基本使用和一些常用代理方法

    - (void)viewDidLoad { [super viewDidLoad]; scrollView = [[UIScrollView alloc] initWithFrame:CGRectMa ...

随机推荐

  1. C# for 和 foreach的执行效率

    for和foreach哪个执行效率快,相信很多人都会说当然是foreach快啊,在我实验之前我也是这么认为的,直到今天.费话不多说,下面是测试的结果,区分Debug和Release,数据采用int[] ...

  2. 洛谷 P2807 三角形计数

    P2807 三角形计数 题目背景 三角形计数(triangle) 递推 题目描述 把大三角形的每条边n等分,将对应的等分点连接起来(连接线分别平行于三条边),这样一共会有多少三角形呢?编程来解决这个问 ...

  3. 关于腾讯云server使用FTP具体配置教程

    本文文件夹:-------------------------------------------------------- [-] 腾讯云server介绍 关于腾讯云server使用感受 作为开发人 ...

  4. jmeter--十三种断言方式介绍

    jmeter中有个元件叫做断言(Assertion),它的作用和loadrunner中的检查点类似: 用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致. 使用断言 ...

  5. 常用协议(SPI, UART, I2C)

    SPI: SPI是全双工的同步串行接口,数据速率可达几Mbps,在一般应用中有4根信号线:MOSI, MISO, SCK, SS. 根据时钟极性(CPOL)及相位(CPHA)不同可以组合成4种工作模式 ...

  6. 关于Altium Designer的BOM,元件清单

    在生成BOM列表的时候,要记得调整BOM的表格的宽度,以免显示不全, 还有就是BOM列表一共有 comment栏 ,description栏,designator栏,footprint栏,libref ...

  7. iOS INVALID_USER_SCODE 定位 用户安全码未通过

    iOS 高德地图API不能定位及INVALID_USER_SCODE问题,有需要的朋友可以参考下. 一.在使用高德地图的API的时候,没有办法实现定位,在这里说一下在真机测试的时候出现没法定位应该注意 ...

  8. 【例题5-8 UVA - 400】Unix ls

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设n个字符串中出现的最长的为len; 最后一列能容纳len个字符,然后前面的列能容纳len+2个字符. 每行最多60个字符. 按照这 ...

  9. IOS RGB颜色转换

    - (UIColor *)getColor:(NSString *)hexColor { unsigned int red,green,blue; NSRange range; range.lengt ...

  10. sqoop 1.4.4-cdh5.1.2快速入门 分类: C_OHTERS 2015-06-06 11:40 208人阅读 评论(0) 收藏

    一.快速入门 (一)下载安装 1.下载并解压 wget http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.4-cdh5.1.2.tar.gz tar - ...