关于iOS TableView的一些笔记及相关

TableView

import “FriendsList.h”

#import “DraFriend.h” #import “FriendGroup.h”

@interface FriendsList()

pragma mark 加载数据

-(void) initData
{
_friendGroup = [[NSMutableArray alloc] init];

DraFriend* friend1 = [DraFriend initWithName:@"shaqima" PhoneNumber:@"18650495875" DraId:@"001"];
DraFriend* friend2 = [DraFriend initWithName:@"yize" PhoneNumber:@"15659430805" DraId:@"002"];

FriendGroup* group1 = [FriendGroup initWithGroupname:@"Friends" Discription:@"your fathers" Friends:[NSMutableArray arrayWithObjects:friend1,friend2, nil]];

[_friendGroup addObject:group1];

DraFriend* friend3 = [DraFriend initWithName:@"Tom" PhoneNumber:@"18650495873" DraId:@"003"];
DraFriend* friend4 = [DraFriend initWithName:@"Jerry" PhoneNumber:@"15659430803" DraId:@"004"];

FriendGroup* group2 = [FriendGroup initWithGroupname:@"Family" Discription:@"love home" Friends:[NSMutableArray arrayWithObjects:friend3,friend4, nil]];

[_friendGroup addObject:group2];

DraFriend* friend5 = [DraFriend initWithName:@"shabi" PhoneNumber:@"18650495871" DraId:@"005"];
DraFriend* friend6 = [DraFriend initWithName:@"bendan" PhoneNumber:@"15659430801" DraId:@"006"];

FriendGroup* group3 = [FriendGroup initWithGroupname:@"eggs" Discription:@"a group of sb" Friends:[NSMutableArray arrayWithObjects:friend5,friend6, nil]];

[_friendGroup addObject:group3];

}

pragma mark 数据源方法

pragma mark 返回分组数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSLog(@”计算分组数”);
return _friendGroup.count;
}

pragma mark 返回组的人数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
FriendGroup * group = _friendGroup[section];
return [[group group_friends] count];
}

pragma mark 返回单元格

-(UITableViewCell*) tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath
{
FriendGroup *group=_friendGroup[indexPath.section];
DraFriend *friend=group.group_friends[indexPath.row];

static NSString *cellIdentifier=@"UITableViewCellIdentifierKey1";
static NSString *cellIdentifierForFirstRow=@"UITableViewCellIdentifierKeyWithSwitch";
UITableViewCell *cell;
if(indexPath.row == 0)
{
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierForFirstRow];
}
else
{
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

}
if(!cell)
{
    if (indexPath.row==0) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierForFirstRow];
        UISwitch *sw=[[UISwitch alloc]init];
        [sw addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView=sw;
    }
        else{
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
            cell.accessoryType=UITableViewCellAccessoryDetailButton;
        }
    }

cell.textLabel.text=friend.name;
cell.detailTextLabel.text=friend.phoneNumber;
cell.accessoryType =  UITableViewCellAccessoryDetailButton;
//生成详细信息标示




if(indexPath.row==0){
    ((UISwitch *)cell.accessoryView).tag=indexPath.section;
} //生成第一个选项上的开关





return cell;

}

pragma mark 返回每组组名_

-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section{
NSLog(@”生成组(组%ld)名称”,(long)section);
FriendGroup* group = _friendGroup[section];
return group.group_name;
}

pragma mark 返回每组的描述

-(NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section{
NSLog(@”生成尾部(组%ld)详情”,section);
FriendGroup* group = _friendGroup[section];
return group.group_description;
}

pragma mark 返回每组标题索引

-(NSArray )sectionIndexTitlesForTableView:(UITableView )tableView{
NSLog(@”生成组索引”);
NSMutableArray *indexs=[[NSMutableArray alloc]init];
for(FriendGroup *group in _friendGroup){
[indexs addObject:group.group_name];
}
return indexs;
}

pragma mark - 代理方法

pragma mark 设置分组标题内容高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section==0){
return 50;
}
return 40;
}

pragma mark 设置每行高度(每行高度可以不一样)

-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{
return 45;
}

pragma mark 设置尾部说明内容高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}

pragma mark 点击行

-(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath{
_selectedIndexPath=indexPath;
FriendGroup *group=_friendGroup[indexPath.section];
DraFriend *draFriend =group.group_friends[indexPath.row];
//创建弹出窗口
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@”System Info” message:[draFriend name] delegate:self cancelButtonTitle:@”Cancel” otherButtonTitles:@”OK”, nil];
alert.alertViewStyle=UIAlertViewStylePlainTextInput; //设置窗口内容样式
UITextField *textField= [alert textFieldAtIndex:0]; //取得文本框
textField.text=draFriend.phoneNumber; //设置文本框内容
[alert show]; //显示窗口
}

pragma mark 窗口的代理方法,用户保存数据

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//当点击了第二个按钮(OK)
if (buttonIndex==1) {
UITextField *textField= [alertView textFieldAtIndex:0];
//修改模型数据
FriendGroup *group=_friendGroup[_selectedIndexPath.section];
DraFriend *draFriend=group.group_friends[_selectedIndexPath.row];
draFriend.phoneNumber=textField.text;
//刷新表格
NSArray *indexPaths=@[_selectedIndexPath];//需要局部刷新的单元格的组、行
[_tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];//后面的参数代码更新时的动画
}
}

pragma mark 切换开关转化事件

-(void)switchValueChange:(UISwitch *)sw{
NSLog(@”section:%i,switch:%i”,sw.tag, sw.on);
}

@end

1   由于此时我们需要两种UITableViewCell样式,考虑到性能我们需要在缓存池缓存两种Cell。
2   UISwitch继承于UIControl而不是UIView(当然UIControl最终也是继承于UIView),继承于UIControl的控件使用addTarget添加对应事件而不是代理,同时有“是否可用”、“是否高亮”、“是否选中”等属性;
3   上面代码中如果有些UITableViewCell的UISwitch设置为on当其他控件重用时状态也是on,解决这个问题可以在模型中设置对应的属性记录其状态,在生成cell时设置当前状态(为了尽可能简化上面的代码这里就不再修复这个问题);

pragma mark 删除操作

//实现了此方法向左滑动就会显示删除按钮
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath{
KCContactGroup *group =_contacts[indexPath.section];
KCContact *contact=group.contacts[indexPath.row];
if (editingStyle==UITableViewCellEditingStyleDelete) {
[group.contacts removeObject:contact];
//考虑到性能这里不建议使用reloadData
//[tableView reloadData];
//使用下面的方法既可以局部刷新又有动画效果
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

    //如果当前组中没有数据则移除组刷新整个表格
    if (group.contacts.count==0) {
        [_contacts removeObject:group];
        [tableView reloadData];
    }
}

}

pragma mark 取得当前操作状态,根据不同的状态左侧出现不同的操作按钮

-(UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath{
if (_isInsert) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}

pragma mark 编辑操作(删除或添加)

//实现了此方法向左滑动就会显示删除(或添加)图标
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath{
KCContactGroup *group =_contacts[indexPath.section];
KCContact *contact=group.contacts[indexPath.row];
if (editingStyle==UITableViewCellEditingStyleDelete) {
[group.contacts removeObject:contact];
//考虑到性能这里不建议使用reloadData
//[tableView reloadData];
//使用下面的方法既可以局部刷新又有动画效果
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

    //如果当前组中没有数据则移除组刷新整个表格
    if (group.contacts.count==0) {
        [_contacts removeObject:group];
        [tableView reloadData];
    }
}else if(editingStyle==UITableViewCellEditingStyleInsert){
    KCContact *newContact=[[KCContact alloc]init];
    newContact.firstName=@"first";
    newContact.lastName=@"last";
    newContact.phoneNumber=@"12345678901";
    [group.contacts insertObject:newContact atIndex:indexPath.row];
    [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];//注意这里没有使用reladData刷新
}

}

pragma mark 排序

//只要实现这个方法在编辑状态右侧就有排序图标
-(void)tableView:(UITableView )tableView moveRowAtIndexPath:(NSIndexPath )sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
KCContactGroup *sourceGroup =_contacts[sourceIndexPath.section];
KCContact *sourceContact=sourceGroup.contacts[sourceIndexPath.row];
KCContactGroup *destinationGroup =_contacts[destinationIndexPath.section];

[sourceGroup.contacts removeObject:sourceContact];
if(sourceGroup.contacts.count==0){
    [_contacts removeObject:sourceGroup];
    [tableView reloadData];
}

[destinationGroup.contacts insertObject:sourceContact atIndex:destinationIndexPath.row];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值