UITableView

stroyboard中创建的tableViewController 和自定义的cell,在使用的时候需要通过 stroyboard 创建控制器,不然直接通过代码生成的不识别自定的cell


直接向cell的contentView上面添加subView
  这是比较简单的一种的,根据布局需要我们可以在不同的位置添加subView。但是此处需要注意:所有添加的subView都最好设置为不透明的,因为如果subView是半透明的话,view图层的叠加将会花费一定的时间,这会严重影响到效率。同时如果每个cell上面添加的subView个数过多的话(通常超过3,4个),效率也会受到比较大的影响。  


根据view  获得cell 如果是视图拖出来的话应该都是这种,代码中:如果用cell.contentView.add也是, 如果用cell.add则少一个superview

http://blog.sina.com.cn/s/blog_7c336a830101hoaj.html

  1. UITableViewCell *cell (UITableViewCell *)[[[btn superview] superview] superview];  
  2. NSIndexPath *indexPath [_myTableView indexPathForCell:cell];  
  3. NSLog(@"indexPath is %i",indexPath.row);  

解释:第一句代码中的[btn superview]是shopCell 的contentView,第二个superview是shopCell自己本身的cell,第三个superview是UITableView的cell,,注意不要弄混淆了。

                             


  //在tableViewController 自带一个tableView  不是uiView 

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

取消选中状态

tableView.deselectRowAtIndexPath(indexPath, animated:true)


也可自己实现  遵守协议 <UITableViewDataSource,UITableViewDelegate>

         对应的代理          ableView.dataSource = self;   tableView.delegate = self;


#pragma mark 返回组数 默认为一

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.dict allKeys].count;

}


#pragma mark 返回分组名

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.keyArray objectAtIndex:section];

}


#pragma mark 返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [self.keyArray objectAtIndex:section];
    NSArray *array = [self.dict objectForKey:key];
    return array.count;

}


#pragma mark 返回cell 必须实现
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //cell重用机制

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flag"];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"flag"] autorelease];
    }
    
    NSString *key = [self.keyArray objectAtIndex:indexPath.section];
    NSArray *array = [self.dict objectForKey:key];
    Person *person = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = person.name;
    cell.detailTextLabel.text = person.phoneNum;
    cell.imageView.image = [UIImage imageNamed:person.imageName];
    return cell;
}

#pragma mark 索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.keyArray;
}


#pragma mark cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;

}


-------------------------------------------********************************************************-------------------------------------------

#pragma mark UITableView编辑


#pragma mark 指定编辑行
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark 指定编辑类型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //返回删除 对应提交结果里面的/*.....*/处理方法
//    return UITableViewCellEditingStyleDelete;


    //返回插入
    return UITableViewCellEditingStyleInsert;
    
}

#pragma mark 提交编辑结果
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    //先删除数据源
    NSMutableArray *deleteArray = [self.dict objectForKey:self.keyArray[indexPath.section]];
    [deleteArray removeObjectAtIndex:indexPath.row];
    
    //删除UI上的cell
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    
    //判断分组是否没有cell
    if (deleteArray.count <= 0) {
        [self.dict removeObjectForKey:[self.keyArray objectAtIndex:indexPath.section]];
        [self.keyArray removeObjectAtIndex:indexPath.section];
        
        //删除ui上的分组
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
    }
    */
   

   //-------------插入
    //添加。。。
    Person *person = [Person personWithName:@"航地" Age:20 Sex:@"男" PhoneNum:@"101010" ImageName:@"1.png"];
    
    NSMutableArray *array = [self.dict objectForKey:[self.keyArray objectAtIndex:indexPath.section]];
  

// [array addObject:person];

   // 插入 不能使用添加 否则会联系人添加到最后 还需要滑动刷新
    [array insertObject:self.person atIndex:indexPath.row];
    //刷新数据 没有动画
//    [self.rootView.tableView reloadData];


    //有动画  但是需要滑动 UI才会更新数据
    [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    
}

#pragma mark barButtonItem 右上角
-(void)buttonAction:(UIBarButtonItem *)sender
{
    
    if ([sender.title isEqualToString:@"编辑"]) {
        sender.title = @"完成";
        [self.rootView.tableView setEditing:YES animated:YES];//置为编辑状态
    }else{
        sender.title = @"编辑";
        [self.rootView.tableView setEditing:NO animated:YES];
    }
}

#pragma mark 修改删除显示的字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}


#pragma mark 完成移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //先改变数据源
    NSMutableArray *moveArray = [self.dict
                                 objectForKey:[self.keyArray objectAtIndex:sourceIndexPath.section]];
    
    Person *temp = [moveArray objectAtIndex:sourceIndexPath.row];
    [temp retain]; //对应引用计数加1 删除掉在添加到后面 实现移动
    [moveArray removeObjectAtIndex:sourceIndexPath.row];
    [moveArray insertObject:temp atIndex:destinationIndexPath.row];

    
    
    //交换数组里的值 只是交换 但不是移动 会使得相应位置上的cell交换  用上面
//    [moveArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    
    //改变UI
    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

}

#pragma mark 检测移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    //同一分组就移动到目的处
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }
    //不同分组就移动到源处
    return sourceIndexPath;
}


++++++++++++++++++++++++++++++++++++++++++++

注意看图二划动以后的"Delete",你有没有想把这个东东改掉的冲动呢?比如改成:下载?其实很简单,其实下面这个代理方法:

++++++++++++++++++++++++++++++++++++++++++++


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ 
return  @ "下载" ;
}



++++++++++++++++++++++++++++++++++++++++++++

自定义cell之间的线条

++++++++++++++++++++++++++++++++++++++++++++

先将UITableView中将分割线样式改为None 

tableView.separatorStyle = UITableViewCellSeparator<wbr>StyleNone;</wbr>

自定义cell并重写下面方法:

- (void)drawRect:(CGRect)rect {

    CGContextRef context =UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [UIColorclearColor].CGColor);CGContextFillRect(context, rect);

    //上分割线,

    CGContextSetStrokeColorWithColor(context, [UIColorredColor].CGColor);CGContextStrokeRect(context,CGRectMake(5, -1, rect.size.width - 10, 1));

    //下分割线

    CGContextSetStrokeColorWithColor(context, [UIColorblueColor].CGColor);CGContextStrokeRect(context,CGRectMake(5, rect.size.height, rect.size.width - 10, 1));

    

}


UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0, 0, 320, 10)];

        tableView.tableFooterView = view; //可以将View高度设置为1;就可以覆盖掉最后一个的线

        tableView.tableHeaderView = view;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值