{
NSIndexPath *seletedRow;
NSIndexPath *currentRow;
NSIndexPath *pastRow;
UITableViewCell *pastCell;
NSMutableArray *_datalist;
}
@end
@implementation ViewController
(void)viewDidLoad
{
[super viewDidLoad];
_datalist=[NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"C",@"C",@"C",@"C", nil];
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _datalist.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//记录上一个Cell
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [_datalist objectAtIndex:[indexPath row]];
//调用点击cell这后进行的条件判断(抽取到一个方法中)
[self selectCondition:cell indexPath:indexPath];
return cell;
}
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
currentRow = indexPath;
NSArray *indexPathArray = [NSArray arrayWithObject:indexPath];
[tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationMiddle];
}
-(void)selectCondition:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath
{
//获取上一个cell,然后再将上一个cell隐藏的控件显示出来
if (pastRow != nil && ![pastRow isEqual:cell]) {
pastRow = nil;
}
//再次选中该行隐藏这个view
if ([currentRow isEqual:seletedRow]) {
NSLog(@"[currentRowAtIndexPath isEqual:seletedRowAtIndexPath] 条件成立 cellForRowAtIndexPath");
//再次点击的时候,将所有的记录行号的信息全部消除
seletedRow = nil;
currentRow = nil;
pastRow = nil;
}
//选中一行加载一个view
if (currentRow !=nil && [currentRow isEqual:indexPath]) {
//点击了就记录当前的cell,用于下一次点击的时候调用
pastCell = cell;
NSLog(@"past的测试:%@",pastCell);
seletedRow = currentRow;
pastRow = indexPath;
//调用子控件设置的方法
[self setSubViewAndButton:cell];
}
}
(void)setSubViewAndButton:(UITableViewCell *)cell
{
//展开的时候创建uiview
UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(0, 50, self.view.frame.size.width, 50);
//view.backgroundColor=[UIColor grayColor];
//view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"back.jpg"]];
view.backgroundColor = [UIColor yellowColor];
[cell.contentView addSubview:view];
NSLog(@"currentRowAtIndexPath !=nil && [currentRowAtIndexPath isEqual:indexPath] 条件成立 cellForRowAtIndexPath ");
//在动态创建的view上面添加想要的子控件(uibutton,可以设置文字,可以设置背景图片)
//呼叫按钮
UIButton *btn = [[UIButton alloc] init];
//[callOutButton setTitle:@"呼叫" forState:UIControlStateNormal];
//[btn setBackgroundImage:[UIImage imageNamed:@"cat.png"] forState:UIControlStateNormal];
btn.frame = CGRectMake(20, 5, 30, 30);
//callOutButton.backgroundColor = [UIColor redColor];
[view addSubview:btn];
//给view内部的按钮设置外部的访问接口(看需不需要)
//给按钮添加监听事件
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
}
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"heightForRowAtIndexPath");
if (currentRow !=nil && [currentRow isEqual:indexPath]) {
if ([currentRow isEqual:seletedRow]) {
tableView.scrollEnabled = YES; //点击以后:合并起来tableview可以滚动
return 50;
}
tableView.scrollEnabled = NO; //点击之后:展开后tableview不可以滚动
return 100;
}
return 50;
}
(void)btnAction:(UIButton *)button
{
NSLog(@"////");
[_datalist removeObjectAtIndex:currentRow.row];
[self.MyTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentRow] withRowAnimation:UITableViewRowAnimationLeft];
}
各位大大,这是我自己做的一个可以点击Cell 改变宽度的DEMO 。 点击了可以实现下拉出一个View 再点击能收起来。点击另外一个Cell自动把上一个Cell收起隐藏,Xcode5.0的时候还好好的 ,升级5.1 点击Cell 不能实现Cell 的收起隐藏了,小弟这两天弄这个急死了,有大神能帮我看看是不是代码有问题,还是升级了Xcode 5.1 的问题啊? 在线等啊,急死了