点击Cell插入菜单栏
来源:互联网 发布:数控铣床编程实例带图 编辑:程序博客网 时间:2024/06/08 06:35
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
int numOfRows;
}
@property(retain, nonatomic) NSIndexPath *selectIndex;
@property (retain, nonatomic) IBOutlet UITableView *myTableView;
@property (retain, nonatomic) NSMutableArray *dataArray;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myTableView, dataArray;
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectIndex = nil;
dataArray = [[NSMutableArray alloc] init];
for (int i=0; i<20; i++) {
[dataArray addObject:[NSNumber numberWithInt:i+1]];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//选中之后的cell的高度
if (indexPath.row == self.selectIndex.row && self.selectIndex != nil){
return 120;
}
else
return 55;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//判断cell是否处于选中状态,并且用不同的identifier标记重用;
//选中的cell改变高度,同时在cell的下方添加背景图片和需要的button;
if (indexPath.row == self.selectIndex.row &&self.selectIndex!= nil){ //选中状态
static NSString *identifier_ = @"cell_";
UITableViewCell *cell_ = [tableView dequeueReusableCellWithIdentifier:identifier_];
if (cell_ == nil){
cell_ = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier_] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
label.tag = 10;
[cell_.contentView addSubview:label];
[label release];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 48, 320, 70)];
image.image = [UIImage imageNamed:@"arrawBg.png"];
[cell_.contentView addSubview:image];
[image release];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(10, 75, 60, 30);
[btn1 setTitle:@"leftBtn" forState:UIControlStateNormal];
btn1.tag = 11;
[btn1 addTarget:self action:@selector(leftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell_.contentView addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(90, 75, 60, 30);
[btn2 setTitle:@"rightBtn" forState:UIControlStateNormal];
btn2.tag = 12;
[btn2 addTarget:self action:@selector(rightButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell_.contentView addSubview:btn2];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn3.frame = CGRectMake(170, 75, 60, 30);
[btn3 setTitle:@"Cancle" forState:UIControlStateNormal];
btn3.tag = 13;
[btn3 addTarget:self action:@selector(cancleButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell_.contentView addSubview:btn3];
UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn4.frame = CGRectMake(250, 75, 60, 30);
[btn4 setTitle:@"Delete" forState:UIControlStateNormal];
btn4.tag = 14;
[btn4 addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell_.contentView addSubview:btn4];
}
cell_.selectionStyle=UITableViewCellSelectionStyleNone;
UILabel *label = (UILabel*)[cell_.contentView viewWithTag:10];
label.text = [NSString stringWithFormat:@"Row %d", [[dataArray objectAtIndex:indexPath.row] intValue]];
return cell_;
}
else{ //非选中状态
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
label.tag = 10;
[cell.contentView addSubview:label];
[label release];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UILabel *label = (UILabel*)[cell.contentView viewWithTag:10];
label.text = [NSString stringWithFormat:@"Row %d", [[dataArray objectAtIndex:indexPath.row] intValue]];
return cell;
}
return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (!self.selectIndex){
self.selectIndex = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.selectIndex]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
else{
BOOL selectTheSameRow = indexPath.row == self.selectIndex.row? YES:NO;
//两次点击不同的cell
if (!selectTheSameRow){
//收起上次点击展开的cell;
NSIndexPath *tempIndexPath = [self.selectIndex copy];
self.selectIndex = nil;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tempIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//展开新选择的cell;
self.selectIndex = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.selectIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else{
//若点击相同的cell,收起cell
self.selectIndex = nil;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
[myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
}
-(void)leftButtonClicked:(UIButton*)button{
NSIndexPath *indexPath;
if ([button.superview.superview isKindOfClass:[UITableViewCell class]]){
UITableViewCell *cell = (UITableViewCell*)button.superview.superview;
indexPath= [myTableView indexPathForCell:cell];
}
NSLog(@"Clicked leftButton of ROW:%d ", indexPath.row);
}
-(void)rightButtonClicked:(UIButton*)button{
NSIndexPath *indexPath;
if ([button.superview.superview isKindOfClass:[UITableViewCell class]]){
UITableViewCell *cell = (UITableViewCell*)button.superview.superview;
indexPath= [myTableView indexPathForCell:cell];
}
NSLog(@"Clicked rightButton of ROW:%d ", indexPath.row);
}
-(void)cancleButtonClicked:(UIButton*)button{
NSIndexPath *indexPath;
if ([button.superview.superview isKindOfClass:[UITableViewCell class]]){
UITableViewCell *cell = (UITableViewCell*)button.superview.superview;
indexPath= [myTableView indexPathForCell:cell];
}
//收起展开的cell
self.selectIndex = nil;
[myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(void)deleteButtonClicked:(UIButton*)button{
NSIndexPath *indexPath;
if ([button.superview.superview isKindOfClass:[UITableViewCell class]]){
UITableViewCell *cell = (UITableViewCell*)button.superview.superview;
indexPath= [myTableView indexPathForCell:cell];
}
self.selectIndex = nil;
[dataArray removeObjectAtIndex:indexPath.row];
[myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationMiddle];//自行选择动画效果
}
@end
- 点击Cell插入菜单栏
- UITableView的点击删除或者插入一条Cell
- 菜单栏点击展开
- MFC基于对话框上插入MENU菜单栏并点击菜单弹出新窗口
- 关于左侧菜单栏,点击二级菜单栏展开
- UITableView点击展开cell
- 取消cell点击效果
- Cell点击变灰色
- iOS cell点击效果
- .net重写菜单栏点击事件
- 点击菜单栏出现的问题
- 点击菜单栏实现内容切换
- tableview插入cell
- 点击cell显示cell的细节
- 点击cell显示cell的细节
- 点击cell显示cell的细节
- 点击cell显示cell的细节
- 点击cell显示cell的细节
- Cocos2d-x如何播放音乐和音效
- 【帧内预测】8.4.4.2.2 Reference sample substitution process for intra sample prediction
- native2ascii将中文国际化
- Lamda演算简介
- SYBASE常用函数
- 点击Cell插入菜单栏
- Head First设计模式-模板方法模式
- 巨强的安全知识库
- Object c 基础知识
- 《推荐系统实践》读书笔记----第二章
- Object-C Categories和Protocols
- 2013、2014 U.S.NEWS美国大学排名榜
- i+=1和i=i+1的区别
- hdu 3687 National Day Parade