一个tableview的自带动画

最近在实习,这是最近老师叫做的项目里面的一个东西,先以为很难,做了很久也没做出来,后面老师讲了之后发现其实很简单,现在分享上来给大家。

说一下他的主要逻辑,其实就是设置了几个section ,section的headview需要自定义。。存了几个字典在datasoure(也就是一个数组当中)。字典里面有个bool值判断是否展开section,也就是是否添加cell进section里面。添加进去的时候tableview自带了动画所以看起来就跟上面一样了。删除同理,只是把cell移除了而已。逻辑真的很简单。因为最近很忙,就直接把项目的代码贴上了。没有注释的地方也请多多包涵。

[super viewDidLoad];
    //
    [self setEdgesForExtendedLayout:UIRectEdgeNone];//避免布局上移

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    //    _dataSource = @{@"面试": @[@"自我介绍", @"如何回答面试官的问题"] ,
    //                    @"薪资": @[@"谈钱牢记四句话", @"面试获高薪资的实战技巧"],
    //                    @"简历": @[@"HR是这样看简历的"]};

    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.backgroundColor = [UIColor whiteColor];

    _dataSource = [[NSMutableArray alloc] init];

    //初始化一条数
    NSDictionary * oraldic1 = @{@"title": @"自我介绍"};
    NSDictionary * oraldic2 = @{@"title": @"如何回答面试官的问题"};

    NSArray * oralarray = @[oraldic1, oraldic2];

    NSMutableDictionary * dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:oralarray forKey:@"detail"];
    [dictionary setObject:@(NO) forKey:@"isFold"];
    [dictionary setObject:@"面试" forKey:@"title"];

    //将数据加入数据源中
    [_dataSource addObject:dictionary];

    //初始化第二条数据
    NSDictionary * saldic1 = @{@"title": @"谈钱牢记四句话"};
    NSDictionary * saldic2 = @{@"title": @"面试获高薪资的实战技巧"};

    NSArray * salarray = @[saldic1, saldic2];
    NSMutableDictionary * salDictionary = [[NSMutableDictionary alloc] init];
    [salDictionary setObject:salarray forKey:@"detail"];
    [salDictionary setObject:@(NO) forKey:@"isFold"];
    [salDictionary setObject:@"薪资" forKey:@"title"];

    [_dataSource addObject:salDictionary];

       NSDictionary *[email protected]{@"title":@"HR时这样看简历的"};
      NSArray *arr=@[dic3];
    NSMutableDictionary *saldic=[[NSMutableDictionary alloc]init];
    [saldic setObject:arr forKey:@"detail"];
    [saldic setObject:@(NO) forKey:@"isFold"];
    [saldic setObject:@"简历" forKey: @"title"];
   [_dataSource addObject:saldic];

    //配置tableview
    self.tableView.rowHeight = 40;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //添加刷新功能
    self.refreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    self.refreshControl.tintColor = [UIColor redColor];
    [self.refreshControl addTarget:self action:@selector(doRefreshProcess) forControlEvents:UIControlEventValueChanged];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.

    return [_dataSource count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.

    //得到header状态
    BOOL flag = [_dataSource[section][@"isFold"] boolValue];
    return flag ? [_dataSource[section][@"detail"] count] : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * identify = @"reuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    NSString * key = _dataSource[indexPath.section][@"detail"][indexPath.row][@"title"];

    cell.textLabel.text = key;

    return cell;
}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return _dataSource[section][@"title"];
//}

//创建tableview 头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionIdentifier = @"mySection";
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:sectionIdentifier];
    headerView.userInteractionEnabled = YES;
    if (!headerView) {
        //大标题label
        headerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:sectionIdentifier];
        headerView.contentView.backgroundColor = [UIColor whiteColor];
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 50)];
        textLabel.tag = 201;
        textLabel.textColor = [UIColor blackColor];
        textLabel.backgroundColor = [UIColor whiteColor];
        textLabel.font = [UIFont boldSystemFontOfSize:20];
        [headerView.contentView addSubview:textLabel];

        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 45, 375, 1)];
        lineLabel.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
        [headerView.contentView addSubview:lineLabel];

        //展开按钮
        UIButton *openBtn = [[UIButton alloc] initWithFrame:CGRectMake(330, 5, 40, 40)];
        openBtn.tag = 202;
        openBtn.backgroundColor = [UIColor redColor];
        [openBtn.titleLabel setFont:[UIFont systemFontOfSize:20]];
        [openBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [openBtn setImage:[UIImage imageNamed:@"inter_open"] forState:UIControlStateNormal];
        [openBtn setImage:[UIImage imageNamed:@"inter_close"] forState:UIControlStateSelected];
        [openBtn addTarget:self action:@selector(doFoldAndUnfold:) forControlEvents:UIControlEventTouchUpInside];
        [headerView.contentView addSubview:openBtn];
    }

    UILabel *textLabel = (UILabel *)[headerView viewWithTag:201];
    textLabel.text = _dataSource[section][@"title"];
    UIButton *sectionBtn = (UIButton *)[headerView viewWithTag:202];
    //得到展开状态
    BOOL flag = [_dataSource[section][@"isFold"] boolValue];
    sectionBtn.selected = flag ? YES : NO;
    sectionBtn.tag = 300 + section;//重新设定tag值

    return headerView;
}

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

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LMdetailController *detail=[[LMdetailController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)doFoldAndUnfold:(UIButton *)sender
{
    //修改按钮点击状态
    sender.selected = !sender.selected;

    NSInteger section = sender.tag - 300;//得到哪组
    //得到数据
    //首先确定展开之后多少行
    NSMutableArray * indexArray = [[NSMutableArray alloc] init];
    NSInteger count = [_dataSource[section][@"detail"] count];
    for (int i = 0; i < count; i ++) {
        NSIndexPath * path = [NSIndexPath indexPathForRow:i inSection:section];
        [indexArray addObject:path];
    }

    //同步数据源中的展开状态
    [_dataSource[section] setObject:@(sender.selected) forKey:@"isFold"];

    //开始展开动画
    if (sender.selected) {
        [self.tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop];
    } else {
        [self.tableView deleteRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop];
    }

}

- (void)doRefreshProcess
{
    [self.refreshControl beginRefreshing];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"刷新中..."];
    NSLog(@"开始刷新");

    //模拟网络请求延时处理
    [self performSelector:@selector(refreshUserInterface) withObject:nil afterDelay:1.0];
}

- (void)refreshUserInterface
{

    [self.refreshControl endRefreshing];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    NSLog(@"结束");
    //重新刷新tableview
    [self.tableView reloadData];
}
时间: 2024-10-10 04:59:51

一个tableview的自带动画的相关文章

一个加载时带动画效果的ListBoxItem

原文:一个加载时带动画效果的ListBoxItem 今天我们来谈一下ListBoxItem这个控件,ListBoxItem是直接从ContentControl继承而来的,所以可以添加到任何具有Content属性的控件中去,常见的ListBoxItem可以放到ListBox中,也可以放到ItemsControl中去,ListBoxItem可以横向和TreeViewItem进行比较,只不过TreeViewItem是直接从HeaderedItemsControl继承过来的,然后再继承自ItemsCon

如果写一个点击view带动画的下滑展开显示隐藏内容的控件

原理是在onMeasure中得到隐藏内容的高度,点击这个view的时候对隐藏的view startAnimation,让它的高度从0增长到onMeasure得到的这个View的measureHeight 具体这样写: public class ExpandableLayout extends LinearLayout { private Context mContext; private LinearLayout mHandleView; private RelativeLayout mCont

IOS的一个带动画的多项选择的控件(二)

然后我们来写:TypeSelectView 这个比较简单,我们只要只要每个TypeView的位置,然后作为自己的subview就好了 @interface TypeSelectView : UIView @property(nonatomic) BOOL bShown; @property(nonatomic, strong) TypeView* curSelectedView; -(id)initWithFrame:(CGRect)frame searchType:(int)type; @en

IOS的一个带动画的多项选择的控件(一)

先上效果图: 这个程序分2个层次,一个是顶部的带UITextField的bar,一个是下拉选择的view,下拉选择的view带有4个自定义的UIView 我们先定义一个UIViewController叫MyViewController,然后顶部的bar叫TopBarView,下拉选择的view叫TypeSelectView,像UIButton的自定义的view叫做TypeView TypeView有两种状态,如果手指触摸到的item就是选中状态,所以TypeSelectView应该有个属性表示当

写一个android带动画效果的TabHost(类似微博客户端的切换效果)

先上图: 这个Layout是: <?xml version="1.0" encoding="UTF-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" andro

一个带动画的dialog

根据项目需求需要在加载的时候加入一个带动画效果的进度. 最先想到的就是 自定义一个dialog 再加上一个动画就OK了. so..... public class CMYProgressDialog extends Dialog { public CMYProgressDialog(Context context) { super(context, R.style.CMYProgressDialog); setContentView(R.layout.layout_loading); getWi

ios如果写一个提示带动画的View,可以来引导用户行为

先上图: 这个UIView可以这样写: -(id)initWithFrame:(CGRect)frame backImage:(UIImage*)image msgStr:(NSString*)txt txtColor:(UIColor*)color{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; _paopaoImage = image; _txt = t

收藏一个带动画效果的ScrollViewer以及ScrollBar的模板

原文:收藏一个带动画效果的ScrollViewer以及ScrollBar的模板 这里介绍一个带动画效果的ScrollViewer和ScrollBar,总共分为两个资源字典,直接拿来引用即可: 1 ScrollBarStyle.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft

07---关于动态创建和销毁带动画的UIVew

在我们做开发的过程中经常会遇到在你触发了某个动作之后,需要动态添加一个带动画的UIView,执行完以后就销毁这个UIView #pragma mark 展示最新微博的数目 - (void)showNewStatusCount:(int)count { // 1.创建按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.enabled = NO; // 设置按钮禁用 btn.adjustsImageWhenDisa