xiaoyu_zheng 2014-03-22 06:35
浏览 1183

学生党求教啊 关于IOS TableViewCell

{
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 的问题啊? 在线等啊,急死了

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 想问一下stata17中这段代码哪里有问题呀
    • ¥15 flink cdc无法实时同步mysql数据
    • ¥100 有人会搭建GPT-J-6B框架吗?有偿
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决