UITableView

//必须要的协议
<UITableViewDataSource,UITableViewDelegate]] > 


- (void)viewDidLoad

{
    [superviewDidLoad];
    self.title=@"一窝";
    //添加编辑按钮
    self.navigationItem.rightBarButtonItem=self.editButtonItem;
// Do any additional setup after loading the view, typically from a nib.
    NSMutableArray * arr=[NSMutableArrayarrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m", nil];
    NSMutableArray *arr2=[NSMutableArrayarrayWithObjects:@"啊",@"波",@"次",@"的", nil];
    NSMutableArray *arr3=[NSMutableArrayarrayWithObjects:@"1",@"3",@"2", nil];
    self.selectData=[NSMutableDictionary dictionaryWithObjectsAndKeys:arr,@"英文",arr2,@"中文", arr3,@"数字",nil];
    //self.data=arr;
    UITableView * table=[[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];//简单样式
    //当前的控制器跟table关联
    table.delegate=self;
    table.dataSource=self;
    self.table=table;
    [self.view addSubview:table];
    [table release];
}


#pragma mark --数据源协议中必须实现的两个方法


//每个分组的行数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray * allKeys=[self.selectData.allKeys sortedArrayUsingSelector:@selector(compare:)];
    NSString *key=[allKeys objectAtIndex:section];
    NSMutableArray *da=[self.selectData objectForKey:key];
    return da.count;
}
//返回调用表格的一个单元格
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell=nil;
    //重用
    cell=[self.table dequeueReusableCellWithIdentifier:@"test"];//从队列中取标识为test的单元格
    //没有重用
    if (cell==nil) {
        //新建一个cell,并存入队列中标识为test
        cell=[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"test"] autorelease];        
    }
//    //自定义单元格
//    for(UIView * v in cell.contentView.subviews)
//    {
//        [v removeFromSuperview];
//    }
//    UILabel * lbl=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
//    lbl.text=@"string";
//    [cell.contentView addSubview:lbl];
//    [lbl release];
    
    cell.imageView.image=[UIImageimageNamed:@"book_4.png"];
    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
    
    NSArray * allKeys=[self.selectData.allKeys sortedArrayUsingSelector:@selector(compare:)];
    NSString *key=[allKeys objectAtIndex:indexPath.section];
    NSMutableArray *da=[self.selectData objectForKey:key];
    cell.textLabel.text=[da objectAtIndex:indexPath.row];
    return cell;

}


//总共几组
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    returnself.selectData.count;
}
分组的下面的标题
//-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//{
//    NSArray * allKeys=[self.selectData.allKeys sortedArrayUsingSelector:@selector(compare:)];
//    NSString *key=[allKeys objectAtIndex:section];
//    NSMutableArray *da=[self.selectData objectForKey:key];
//    NSString * foot=[NSString stringWithFormat:@"共%d人",da.count];
//    return foot;
//}
//分区前的标题
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray * allKeys=[self.selectData.allKeys sortedArrayUsingSelector:@selector(compare:)];
    NSString *key=[allKeys objectAtIndex:section];
    //NSMutableArray *da=[self.selectData objectForKey:key];
    return key;
}

//设置索引栏
-(NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSArray * allKeys=[self.dic.allKeys sortedArrayUsingSelector:@selector(compare:)];
    return allKeys;
}
//选中时的事件处理
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.indexPath=indexPath;
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];    
    ContentViewController * cvc=[[ContentViewControlleralloc] init];   
    cvc.preViewControl=self;
    [self.navigationController pushViewController:cvc animated:YES];   
    [cvc release];
    
}
//设置行高,点中时行增高
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.indexPath!=nil&&[self.indexPath isEqual:indexPath]) {
        return 100;
    }
    return 50;
}

#pragma end mark

#pragma mark 表格编辑操作
//设置编辑状态
-(void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.table setEditing:editing animated:animated];
}


//单元格可移动
-(BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    returnYES;
}
//拖拽过程
-(NSIndexPath *) tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    //设置只在该分组中移动
    if (proposedDestinationIndexPath.section!=sourceIndexPath.section) {
        return sourceIndexPath;
    }
    return proposedDestinationIndexPath;
}
//拖拽结束
-(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //重新加载表格
    //[self.table reloadData];
}
//设置每个单元格的编辑样式
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row%2==0) {
        return UITableViewCellEditingStyleDelete;
    }
    else
    {
        return UITableViewCellEditingStyleNone;
    }
}
//在编辑状态点击delete时
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.删除数据
    NSArray * allKeys=[self.selectData.allKeys sortedArrayUsingSelector:@selector(compare:)];
    NSString *key=[allKeys objectAtIndex:indexPath.section];
    NSMutableArray *da=[self.selectData objectForKey:key];
    [da removeObjectAtIndex:indexPath.row];
    //2.删除单元格
    [self.table deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma end mark

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值