一些小知识点的总结(一)

1、cell多选删除
效果图:


一些小知识点的总结(一)_第1张图片
02-FZHDeleteCells.gif

1)、创建每个功能的按钮
OC:

- (void)setupNavigationItem{

    UIBarButtonItem *editBtn = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(buttonDidTouch:)];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(buttonDidTouch:)];
    UIBarButtonItem *deleteBtn = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStyleDone target:self action:@selector(buttonDidTouch:)];
    UIBarButtonItem *selectAllBtn = [[UIBarButtonItem alloc]initWithTitle:@"全选" style:UIBarButtonItemStyleDone target:self action:@selector(buttonDidTouch:)];
    
    editBtn.tag = 1001;
    doneBtn.tag = 1002;
    deleteBtn.tag = 1003;
    self.navigationItem.leftBarButtonItems = @[editBtn,doneBtn,deleteBtn,selectAllBtn];
}

swift:

    func setupNavigationBar() -> Void {
        let editButton = UIBarButtonItem.init(title: "编辑", style: .Done, target: self, action: #selector(buttonDidTouch))
        let doneButton = UIBarButtonItem.init(title: "完成", style: .Done, target: self, action: #selector(buttonDidTouch))
        let deleteButton = UIBarButtonItem.init(title: "删除", style: .Done, target: self, action: #selector(buttonDidTouch))
        let selectAllButton = UIBarButtonItem.init(title: "全选", style: .Done, target: self, action: #selector(buttonDidTouch))
        
        editButton.tag = 1001
        doneButton.tag = 1002
        deleteButton.tag = 1003
        
        self.navigationItem.leftBarButtonItems = [editButton,doneButton,deleteButton,selectAllButton]
    }
    

2)、每个按钮的响应事件
OC:

- (void)buttonDidTouch:(UIButton *)btn{
    if (btn.tag == 1001) {//edit
        self.tableView.editing = YES;
        self.tableView.allowsMultipleSelectionDuringEditing = YES;
    }else if (btn.tag == 1002){//done
        self.tableView.editing = NO;
    }else if (btn.tag == 1003){//delete
        [self.dataSource removeObjectsInArray:self.deleteArray];
        [self.tableView reloadData];
    }else{
        if (self.tableView.editing == NO) {
            return;
        }else{
            for (int i = 0; i < self.dataSource.count; i++) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
            }
            [self.deleteArray addObjectsFromArray:self.dataSource];
        }
    }
}

swift:

    func buttonDidTouch(btn:UIButton) -> Void {
        if btn.tag == 1001 {//编辑
            tableView.editing = true
            tableView.allowsMultipleSelectionDuringEditing = true
        }else if btn.tag == 1002{//完成
            tableView.editing = false
        }else if btn.tag == 1003{//删除
            self.dataSource.removeObjectsInArray(self.deleteArr as [AnyObject])
            tableView.reloadData()
        }else{//全选
            if tableView.editing == false {//如果处于完成状态则直接返回
                return
            }
            for row in 0...self.dataSource.count {
                let indexPath = NSIndexPath.init(forRow: row, inSection: 0)
                tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)
            }
            self.deleteArr.addObjectsFromArray(self.dataSource as [AnyObject])
        }
    }

3)、实现按钮功能的tableView相关协议的方法
OC:

#pragma mark - UITableViewDelegate
//选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.deleteArray addObject:self.dataSource[indexPath.row]];
}
//取消选中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.deleteArray removeObject:self.dataSource[indexPath.row]];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}

swift:

//    设置编辑style
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.init(rawValue: UITableViewCellEditingStyle.Delete.rawValue | UITableViewCellEditingStyle.Insert.rawValue)!
    }
//    选中则添加到删除数组中
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.deleteArr.addObject(self.dataSource[indexPath.row])
    }
//    取消选中在数组中删除
    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        self.deleteArr.removeObject(self.dataSource[indexPath.row])
    }

2、swift声明extension

extension SomeType {
    // 为 SomeType 添加的新功能写到这里
}
eg:
//该extension为UIColor添加了一个输出`i am a color`的方法
extension UIcolor{
  fun printFun{
  print("i am a color")
}
}

2.1、解析Hex color的UIColor的Extension
OC:

+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

//默认alpha值为1
+ (UIColor *)colorWithHexString:(NSString *)color
{
    return [self colorWithHexString:color alpha:1.0f];
}


// Create a color using a hex RGB value
// ex. [UIColor colorWithHexValue: 0x03047F]
+ (UIColor *) colorWithHexValue: (NSInteger) rgbValue {
    return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
                           green:((float)((rgbValue & 0xFF00) >> 8))/255.0
                            blue:((float)(rgbValue & 0xFF))/255.0
                           alpha:1.0];
}

swift:

extension UIColor{
   class func colorWithHexString(color: String,alpha: CGFloat) -> UIColor {
        var colorStr = color.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
        let length = colorStr.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
        //    let length = (colorStr as NSString).length
        if length < 6 {
            return UIColor.clearColor()
        }
        if colorStr.hasPrefix("0X") {
            let index = colorStr.startIndex.advancedBy(2)
            colorStr = colorStr.substringFromIndex(index)
        }
        if colorStr.hasPrefix("#") {
            let index = colorStr.startIndex.advancedBy(1)
            colorStr = colorStr.substringFromIndex(index)
        }
        
        //    red
        //    myString.startIndex.advancedBy(1).. UIColor {
        return self.colorWithHexString(color, alpha: 1.0)
    }
}

3、swift截取字符串

var myString = "abcde"
let myRange = myString.startIndex.advancedBy(1)..

4、NSNumber转NSString

NSString *myString = [NSNumber stringValue];

DEMO地址:https://github.com/fengzhihao123/CommonKnowledgeSummary
PS:今天Wings夺得TI6冠军,毕竟偶数年。CN Dota,Best Dota!!!

你可能感兴趣的:(一些小知识点的总结(一))