UI基础ios 学习iOS个人修养

UITableView自定义多选删除样式

2015-11-26  本文已影响431人  CGPointZero

Model.h
<pre>#import <Foundation/Foundation.h>
@interface Model : NSObject
@property(nonatomic,strong)NSString *desc;
+(instancetype)modelWithDesc:(NSString *)desc;
@end</pre>
Model.m
<pre>#import "Model.h"

@implementation Model

+(instancetype)modelWithDesc:(NSString *)desc
{
Model *m=[Model new];
m.desc=desc;
return m;
}
@end</pre>
CustomCell.h
<pre>#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

@property(nonatomic,strong)UILabel *titleLb;
@property(nonatomic,strong)UIButton *selectBtn;

@end</pre>
CustomCell.m
<pre>#import "CustomCell.h"
#define kWidth ([UIScreen mainScreen].bounds.size.width)

@implementation CustomCell

@interface EditViewController : UIViewController

@end</pre>
EditViewController.m
<pre>#import "EditViewController.h"

import "CustomCell.h"

import "Model.h"

#define kWidth ([UIScreen mainScreen].bounds.size.width)
#define kHeight ([UIScreen mainScreen].bounds.size.height)
#define RGB(r,g,b) ([UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f])

@interface EditViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tbView;
NSMutableArray *_dataArray;
NSMutableArray *_delArray;
}
//底部工具栏
@property(nonatomic,strong)UIView *toolView;

@end

@implementation EditViewController

pragma mark -

pragma mark - UITableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cid=@"cid";
CustomCell *cel=[tableView dequeueReusableCellWithIdentifier:cid];
if(!cel)
cel=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cid];
Model *model=_dataArray[indexPath.row];
cel.titleLb.text=model.desc;

return cel;

}
//编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
//移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//交换数据
[_dataArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

}
//选中时执行的逻辑
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(_tbView.isEditing)
{
Model *m=_dataArray[indexPath.row];

    if(![_delArray containsObject:m])
    {
        [_delArray addObject:m];
        //若选择了所有行,则将全选标记置为是
        if(_delArray.count==_dataArray.count)
        {
            //全选标记置为是
            UIButton *selectAllBtn=(UIButton *)[self.toolView viewWithTag:100];
            selectAllBtn.selected=YES;
        }
        CustomCell *cell=(CustomCell *)[_tbView cellForRowAtIndexPath:indexPath];
        cell.selectBtn.selected=YES;
    }
    else
    {
        [_delArray removeObject:m];
        //全选标记置为否
        UIButton *selectAllBtn=(UIButton *)[self.toolView viewWithTag:100];
        selectAllBtn.selected=NO;
        //置为未选中
        CustomCell *cell=(CustomCell *)[_tbView cellForRowAtIndexPath:indexPath];
        cell.selectBtn.selected=NO;
    }
    
    //刷新删除数Lb
    [self refreshDeleteCountLb];
}

}
@end
</pre>
Appdelegate.m
<pre>
#import "AppDelegate.h"
#import "EditViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
EditViewController *edit=[EditViewController new];
UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:edit];
navi.navigationBar.hidden=YES;
navi.navigationBar.barStyle=UIBarStyleBlack;
self.window.rootViewController=navi;
return YES;
}</pre>
<pre>

效果图
最后附上GitHub源码地址
</pre>
上一篇下一篇

猜你喜欢

热点阅读