IBTTableViewCellInfo.m 35.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
//
//  IBTTableViewCellInfo.m
//  IBTTableViewKit
//
//  Created by Xummer on 15/1/5.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#define IBT_CELL_HEIGHT_DEFAULT                         (44.0f)
#define IBT_CELL_INNER_GAP                              (10.0f)

#define IBT_CELL_TITLE_LABEL_TAG                        (9999)
#define IBT_CELL_TITLE_FONT_SIZE_DEFAULT                (17.0f)
#define IBT_CELL_TITLE_DETAIL_FONT_SIZE_DEFAULT         (16.0f)
#define IBT_CELL_TITLE_COLOR_DEFAULT                    [UIColor blackColor]

#define IBT_CELL_DETAIL_FONT_SIZE_DEFAULT               (13.0f)
#define IBT_CELL_DETAIL_COLOR_DEFAULT                   [UIColor lightGrayColor]

#define IBT_CELL_LEFT_LABEL_MIN_WIDTH                   (1.0f)
#define IBT_CELL_LEFT_FONT_SIZE_DEFAULT                 (15.0f)
#define IBT_CELL_LEFT_COLOR_DEFAULT                     [UIColor lightGrayColor]

#define IBT_CELL_RIGHT_LABEL_MIN_WIDTH                  (20.0f)
#define IBT_CELL_RIGHT_FONT_SIZE_DEFAULT                (15.0f)
#define IBT_CELL_RIGHT_COLOR_DEFAULT                    [UIColor lightGrayColor]

#define IBT_CELL_URL_DEFAULT_COLOR                      [UIColor colorWithRed:0.341176 green:0.41960 blue:0.584314 alpha:1]

#define IBT_CELL_TEXTFIELD_MIN_WIDTH                    (40.0f)
#define IBT_CELL_TEXTFIELD_DEFAULT_HEIGHT               (30.0f)

#define IBT_CELL_LEFT_IMAGE_V_PADDING                   (4.0f)

#import "IBTTableViewCellInfo.h"
#import "IBTTableViewCell.h"
#import "IBTUILabel.h"
#import "IBTBadgeView.h"

@implementation IBTTableViewCellInfo

#pragma mark - Class Method
+ (IBTTableViewCellInfo *)normalCellForTitle:(NSString *)title rightValue:(NSString *)rightValue
{
    return [[self class] normalCellForSel:nil target:nil title:title rightValue:rightValue imageName:nil accessoryType:UITableViewCellAccessoryNone];
}

+ (IBTTableViewCellInfo *)normalCellForTitle:(NSString *)title rightValue:(NSString *)rightValue
                                   imageName:(NSString *)imgName
{
    return [[self class] normalCellForSel:nil target:nil title:title rightValue:rightValue imageName:imgName accessoryType:UITableViewCellAccessoryNone];
}

+ (IBTTableViewCellInfo *)normalCellForSel:(SEL)sel target:(id)target
                                     title:(NSString *)title
                                rightValue:(NSString *)rightValue
                             accessoryType:(UITableViewCellAccessoryType)type
{
    return [[self class] normalCellForSel:sel target:target title:title rightValue:rightValue imageName:nil accessoryType:type];
}

+ (IBTTableViewCellInfo *)normalCellForSel:(SEL)sel target:(id)target
                                     title:(NSString *)title
                             accessoryType:(UITableViewCellAccessoryType)type;
{
    return [[self class] normalCellForSel:sel target:target title:title rightValue:nil accessoryType:type];
}

+ (IBTTableViewCellInfo *)normalCellForSel:(SEL)sel target:(id)target
                                     title:(NSString *)title
                                rightValue:(NSString *)rightValue
                                 imageName:(NSString *)imgName
                             accessoryType:(UITableViewCellAccessoryType)type
{
    IBTTableViewCellInfo *cellInfo = [[IBTTableViewCellInfo alloc] init];
    cellInfo.makeTarget = cellInfo;
    cellInfo.makeSel = @selector(makeNormalCell:);
    cellInfo.actionTarget = target;
    cellInfo.actionSel = sel;
    cellInfo.accessoryType = type;
    cellInfo.cellStyle = UITableViewCellStyleSubtitle;
    cellInfo.selectionStyle = UITableViewCellSelectionStyleBlue;
    cellInfo.fCellHeight = IBT_CELL_HEIGHT_DEFAULT;
    
    if (title) {
        [cellInfo addUserInfoValue:title forKey:CInfoTitleKey];
    }
    
    if (rightValue.length > 0) {
        [cellInfo addUserInfoValue:rightValue forKey:CInfoRightValueKey];
    }
    
    if (imgName.length > 0) {
        [cellInfo addUserInfoValue:imgName forKey:CInfoImageNameKey];
    }
    
    return cellInfo;
}

+ (IBTTableViewCellInfo *)badgeRightCellForSel:(SEL)sel target:(id)target
                                         title:(NSString *)title badge:(id)badge rightValue:(NSString *)rightValue
                                     imageName:(NSString *)name
{
    IBTTableViewCellInfo *cellInfo =
    [[self class] badgeCellForSel:sel target:target title:title badge:badge rightValue:rightValue imageName:name];
    
    [cellInfo addUserInfoValue:@( YES ) forKey:CInfoBadgeAlignmentRightKey];
    
    return cellInfo;
}

+ (IBTTableViewCellInfo *)badgeCellForSel:(SEL)sel target:(id)target
                                    title:(NSString *)title badge:(id)badge rightValue:(NSString *)rightValue
                                imageName:(NSString *)imageName
{
    IBTTableViewCellInfo *cellInfo =
    [[self class] normalCellForSel:sel target:target title:title rightValue:rightValue imageName:imageName accessoryType:UITableViewCellAccessoryNone];
    
    if (badge) {
        if ([badge isKindOfClass:[NSString class]]) {
            [cellInfo addUserInfoValue:badge forKey:CInfoBadgeKey];
        }
        else if ([badge isKindOfClass:[NSNumber class]]) {
            [cellInfo addUserInfoValue:[NSString stringWithFormat:@"%@", badge] forKey:CInfoBadgeKey];
        }
    }
    
    return cellInfo;
}

+ (IBTTableViewCellInfo *)badgeCellForSel:(SEL)sel target:(id)target
                                    title:(NSString *)title badge:(id)badge rightValue:(NSString *)rightValue
{
    return [[self class] badgeCellForSel:sel target:target title:title badge:badge rightValue:rightValue imageName:nil];
}

+ (IBTTableViewCellInfo *)badgeCellForSel:(SEL)sel target:(id)target
                                    title:(NSString *)title badge:(id)badge
{
    return [[self class] badgeCellForSel:sel target:target title:title badge:badge rightValue:nil imageName:nil];
}

+ (IBTTableViewCellInfo *)switchCellForSel:(SEL)sel target:(id)target
                                     title:(NSString *)title on:(BOOL)bOn
{
    IBTTableViewCellInfo *cellInfo =
    [[self class] normalCellForSel:sel target:nil title:title rightValue:nil imageName:nil accessoryType:UITableViewCellAccessoryNone];
    
    /*
      @{"switch":#"<UISwitch: 0x17ea71d0; frame = (254 6; 51 31); layer = <CALayer: 0x17ea7260>>","title":"Sticky on Top","on":false}
     */
    cellInfo.makeSel = @selector(makeSwitchCell:);
    cellInfo.actionTargetForSwitchCell = target;
    cellInfo.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UISwitch *mSwitch = [[UISwitch alloc] init];
    mSwitch.on = bOn;
    [mSwitch addTarget:cellInfo
                action:@selector(actionSwitchCell:)
      forControlEvents:UIControlEventValueChanged];
    [cellInfo addUserInfoValue:mSwitch forKey:CInfoSwitchKey];
    [cellInfo addUserInfoValue:@( mSwitch.on ) forKey:CInfoSwitchOnKey];
    
    return cellInfo;
}

+ (IBTTableViewCellInfo *)centerCellForSel:(SEL)sel target:(id)target
                                     title:(NSString *)title
{
    IBTTableViewCellInfo *cellInfo =
    [[self class] normalCellForSel:sel target:target title:title accessoryType:UITableViewCellAccessoryNone];
    cellInfo.makeSel = @selector(makeCenterCell:);
    
    return cellInfo;
}

// Open url Inner WebView
+ (IBTTableViewCellInfo *)urlInnerBlueCellForTitle:(NSString *)title leftValue:(NSString *)leftValue url:(id)url
{
    IBTTableViewCellInfo *cellInfo =
    [[self class] normalCellForSel:nil target:nil title:title accessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    cellInfo.actionTarget = cellInfo;
    cellInfo.actionSel = @selector(actionUrlInnerCell);
    
    if (url) {
        [cellInfo addUserInfoValue:url forKey:CInfoURLKey];
    }
    
    if (leftValue) {
        [cellInfo addUserInfoValue:leftValue forKey:CInfoLeftValueKey];
        [cellInfo addUserInfoValue:IBT_CELL_URL_DEFAULT_COLOR forKey:CInfoLeftValueColorKey];
    }
    
    return cellInfo;
}

// Open url Safari (Call open url)
+ (IBTTableViewCellInfo *)urlCellForTitle:(NSString *)title url:(id)url {
    IBTTableViewCellInfo *cellInfo =
    [[self class] normalCellForSel:nil target:nil title:title accessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    cellInfo.actionTarget = cellInfo;
    cellInfo.actionSel = @selector(actionUrlCell);
    
    if (url) {
        [cellInfo addUserInfoValue:url forKey:CInfoURLKey];
    }
    
    return cellInfo;
}

+(IBTTableViewCellInfo *)editorCellForSel:(SEL)sel target:(id)target
                                      tip:(NSString *)tip focus:(BOOL)focus text:(NSString *)text
{
    return [[self class] editorCellForSel:sel target:target title:nil margin:0 tip:tip autoCorrect:YES focus:focus text:text];
}

+(IBTTableViewCellInfo *)editorCellForSel:(SEL)sel target:(id)target
                                      tip:(NSString *)tip focus:(BOOL)focus autoCorrect:(BOOL)correct
                                     text:(NSString *)text
{
    return [[self class] editorCellForSel:sel target:target title:nil margin:0 tip:tip autoCorrect:correct focus:focus text:text];
}

+(IBTTableViewCellInfo *)editorCellForSel:(SEL)sel target:(id)target
                                    title:(NSString *)title margin:(CGFloat)margin
                                      tip:(NSString *)tip focus:(BOOL)focus
                                     text:(NSString *)text
{
    return [[self class] editorCellForSel:sel target:target title:title margin:margin tip:tip autoCorrect:YES focus:focus text:text];
}

+ (IBTTableViewCellInfo *)editorCellForSel:(SEL)sel target:(id)target
                                     title:(NSString *)title
                                    margin:(CGFloat)margin tip:(NSString *)tip
                               autoCorrect:(BOOL)autoCorrect focus:(BOOL)focus
                                      text:(NSString *)text
{
    IBTTableViewCellInfo *cellInfo =
    [[self class] normalCellForSel:sel target:target title:title accessoryType:UITableViewCellAccessoryNone];
    
    /*
     @{"title":"WeChat ID","fEditorLMargin":0,"focus":true,"keyboardType":1,"editor":#"<UITextField: 0x180a6a50; frame = (108 7; 202 30); text = ''; clipsToBounds = YES; opaque = NO; autoresize = W; gestureRecognizers = <NSArray: 0x178e13a0>; layer = <CALayer: 0x178dc930>>","tip":" "}
     */
    
    cellInfo.makeSel = @selector(makeEditorCell:);
    cellInfo.selectionStyle = UITableViewCellSelectionStyleNone;
    cellInfo.autoCorrectionType = autoCorrect ? UITextAutocorrectionTypeYes : UITextAutocorrectionTypeNo;
    
    UITextField *textField = [[UITextField alloc] init];
    [cellInfo addUserInfoValue:textField forKey:CInfoEditorKey];
    
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter addObserver:cellInfo
                   selector:@selector(actionEditorCell:)
                       name:UITextFieldTextDidChangeNotification
                     object:textField];
    
    [cellInfo addUserInfoValue:@( margin ) forKey:CInfoEditorLMarginKey];
    
    if (tip) {
        [cellInfo addUserInfoValue:tip forKey:CInfoEditorTipKey];
    }
    
    if (text) {
        [cellInfo addUserInfoValue:text forKey:CInfoEditorTextKey];
    }
    
    if (focus) {
        [cellInfo addUserInfoValue:@( focus ) forKey:CInfoEditorFocusKey];
    }
    
    return cellInfo;
}

#pragma mark - Life Cycle
- (void)dealloc {
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter removeObserver:self
                          name:UITextFieldTextDidChangeNotification
                        object:nil];
}

#pragma mark - Getter
- (CGFloat)fCellHeight {
    if (self.calHeightTarget &&
        [self.calHeightTarget respondsToSelector:self.calHeightSel]) {
        IMP imp = [self.calHeightTarget methodForSelector:self.calHeightSel];
        CGFloat (*func)(id, SEL, id) = (void *)imp;
        _fCellHeight = func(self.calHeightTarget, self.calHeightSel, self);
    }
    return _fCellHeight;
}

#pragma mark - Make Cell

- (void)makeNormalCell:(IBTTableViewCellInfo *)cellInfo {
    
    IBTTableViewCell *cell = cellInfo.cell;
    for (UIView *v in [cell.contentView subviews]) {
        if (v.tag == IBT_CELL_TITLE_LABEL_TAG) {
            continue;
        }
        
        [v removeFromSuperview];
    }
    
//    cell.editingStyle = self.editStyle;
    cell.selectionStyle = cellInfo.selectionStyle;
    cell.accessoryType = cellInfo.accessoryType;
    
    CGFloat fGap = IBT_CELL_MARGIN;
    CGFloat fLeftX = fGap;
    CGFloat fMaxWidth = CGRectGetWidth(cell.contentView.bounds) - ((cell.accessoryType !=UITableViewCellAccessoryNone || cell.accessoryView) ? 0 : fGap);
    CGFloat fRightX = fMaxWidth;
    CGFloat fH = self.fCellHeight;
    
    // Left Image
    UIImageView *leftImageV = nil;
    NSString *imageName = [cellInfo getUserInfoValueForKey:CInfoImageNameKey];
    if (imageName.length > 0) {
        UIImage *leftImg = [UIImage imageNamed:imageName];
        if (leftImg) {
            leftImageV = [[UIImageView alloc] initWithImage:leftImg];
            leftImageV.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
            CGFloat imageH = leftImg.size.height;
            CGFloat imageW = leftImg.size.width;
            
            CGFloat fMaxImageH = fH - 2 * IBT_CELL_LEFT_IMAGE_V_PADDING;
            
            if (leftImg.size.height > fMaxImageH) {
                imageH = fMaxImageH;
                imageW = leftImg.size.width / leftImg.size.height * imageH;
            }
            
            leftImageV.frame = (CGRect){
                .origin.x = fLeftX,
                .origin.y = (fH - imageH) * .5f,
                .size.width = imageW,
                .size.height = imageH
            };
            
            [cell.contentView addSubview:leftImageV];
            
            fLeftX = CGRectGetMaxX(leftImageV.frame) + fGap;
        }
    }
    
    // Title Label
    UILabel *titleLabel;
    NSString *nsTitle = [cellInfo getUserInfoValueForKey:CInfoTitleKey];
    NSString *nsDetail = [cellInfo getUserInfoValueForKey:CInfoDetailKey];
    
    CGFloat fW = fMaxWidth - fLeftX;
    
    UIView *lastTitleLabel = [cell.contentView viewWithTag:IBT_CELL_TITLE_LABEL_TAG];
    if ([lastTitleLabel isKindOfClass:[IBTUILabel class]]) {
        titleLabel = (IBTUILabel *)lastTitleLabel;
    }
    else {
        titleLabel = [[IBTUILabel alloc] init];
        [cell.contentView addSubview:titleLabel];
    }
    
    titleLabel.textAlignment = NSTextAlignmentLeft;
    id titleColor = [cellInfo getUserInfoValueForKey:CInfoTitleColorKey];
    if ([titleColor isKindOfClass:[UIColor class]]) {
        titleLabel.textColor = titleColor;
    }
    else {
        titleLabel.textColor = IBT_CELL_TITLE_COLOR_DEFAULT;
    }
    
    cell.textLabel.text = nil;
    titleLabel.text = nsTitle;
    
    if (nsDetail) {
        id titleFont = [cellInfo getUserInfoValueForKey:CInfoTitleFontKey];
        if ([titleFont isKindOfClass:[UIFont class]]) {
            titleLabel.font = titleFont;
        }
        else {
            id titleFontSize = [cellInfo getUserInfoValueForKey:CInfoTitleFontSizeKey];
            CGFloat fTitleFontSize =
            ([titleFontSize isKindOfClass:[NSNumber class]]) ?
            [titleFontSize floatValue] :
            IBT_CELL_TITLE_DETAIL_FONT_SIZE_DEFAULT;
            titleLabel.font = [UIFont systemFontOfSize:fTitleFontSize];
        }
        
        // Detail Label
        IBTUILabel *detailLabel = [[IBTUILabel alloc] init];
        detailLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        detailLabel.textAlignment = NSTextAlignmentLeft;
        [cell.contentView addSubview:detailLabel];
        
        id detailFont = [cellInfo getUserInfoValueForKey:CInfoDetailFontKey];
        if ([detailFont isKindOfClass:[UIFont class]]) {
            detailLabel.font = detailFont;
        }
        else {
            id detailFontSize = [cellInfo getUserInfoValueForKey:CInfoDetailFontSizeKey];
            CGFloat fDetailFontSize =
            ([detailFontSize isKindOfClass:[NSNumber class]]) ?
            [detailFontSize floatValue] :
            IBT_CELL_DETAIL_FONT_SIZE_DEFAULT;
            detailLabel.font = [UIFont systemFontOfSize:fDetailFontSize];
        }
        
        id detailValueColor = [cellInfo getUserInfoValueForKey:CInfoDetailColorKey];
        if ([detailValueColor isKindOfClass:[UIColor class]]) {
            detailLabel.textColor = detailValueColor;
        }
        else {
            detailLabel.textColor = IBT_CELL_DETAIL_COLOR_DEFAULT;
        }
        
        detailLabel.text = nsDetail;
        
        fW = fMaxWidth - fLeftX;
        CGSize titleLabelSize = [titleLabel sizeThatFits:CGSizeMake(fW, fH *.5f)];
        if (titleLabelSize.width > fW) {
            titleLabelSize.width = fW;
        }
        CGSize detailLabelSize = [detailLabel sizeThatFits:CGSizeMake(fW, fH * .5f)];
        if (detailLabelSize.width > fW) {
            detailLabelSize.width = fW;
        }
        
        CGFloat fTopY = (fH - titleLabelSize.height - detailLabelSize.height) * .5f;
        
        titleLabel.frame = (CGRect){
            .origin.x = fLeftX,
            .origin.y = fTopY,
            .size = titleLabelSize
        };
        
        fTopY = CGRectGetMaxY(titleLabel.frame);
        detailLabel.frame = (CGRect){
            .origin.x = fLeftX,
            .origin.y = fTopY,
            .size = detailLabelSize
        };
        
        fLeftX = MAX(CGRectGetMaxX(titleLabel.frame), CGRectGetMaxX(detailLabel.frame))  + IBT_CELL_INNER_GAP;
    }
    else {
        
        id titleFont = [cellInfo getUserInfoValueForKey:CInfoTitleFontKey];
        if ([titleFont isKindOfClass:[UIFont class]]) {
            titleLabel.font = titleFont;
        }
        else {
            id titleFontSize = [cellInfo getUserInfoValueForKey:CInfoTitleFontSizeKey];
            CGFloat fTitleFontSize =
            ([titleFontSize isKindOfClass:[NSNumber class]]) ?
            [titleFontSize floatValue] :
            IBT_CELL_TITLE_FONT_SIZE_DEFAULT;
            titleLabel.font = [UIFont systemFontOfSize:fTitleFontSize];
        }
        
        CGSize titleLabelSize = [titleLabel sizeThatFits:CGSizeMake(fW, fH)];
        if (titleLabelSize.width > fW) {
            titleLabelSize.width = fW;
        }
        titleLabel.frame = (CGRect){
            .origin.x = fLeftX,
            .origin.y = (fH - titleLabelSize.height) * .5f,
            .size = titleLabelSize
        };
        
        fLeftX = CGRectGetMaxX(titleLabel.frame) + IBT_CELL_INNER_GAP;
    }
    
    
    // Left Value
    IBTUILabel *leftValueLabel = nil;
    NSString *nsLeftValue = [cellInfo getUserInfoValueForKey:CInfoLeftValueKey];
    if (nsLeftValue.length > 0) {
        leftValueLabel = [[IBTUILabel alloc] init];
        leftValueLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        leftValueLabel.textAlignment = NSTextAlignmentLeft;
        [cell.contentView addSubview:leftValueLabel];
        
        id leftValueFont = [cellInfo getUserInfoValueForKey:CInfoLeftValueFontKey];
        if ([leftValueFont isKindOfClass:[UIFont class]]) {
            leftValueLabel.font = leftValueFont;
        }
        else {
            id leftValueFontSize = [cellInfo getUserInfoValueForKey:CInfoLeftValueFontSizeKey];
            CGFloat fLeftValueFontSize =
            ([leftValueFontSize isKindOfClass:[NSNumber class]]) ?
            [leftValueFontSize floatValue] :
            IBT_CELL_LEFT_FONT_SIZE_DEFAULT;
            leftValueLabel.font = [UIFont systemFontOfSize:fLeftValueFontSize];
        }
        
        id leftValueColor = [cellInfo getUserInfoValueForKey:CInfoLeftValueColorKey];
        if ([leftValueColor isKindOfClass:[UIColor class]]) {
            leftValueLabel.textColor = leftValueColor;
        }
        else {
            leftValueLabel.textColor = IBT_CELL_LEFT_COLOR_DEFAULT;
        }
        
        leftValueLabel.text = nsLeftValue;
        
        fW = MAX(fMaxWidth - fLeftX, IBT_CELL_LEFT_LABEL_MIN_WIDTH);
        CGSize leftLabelSize = [leftValueLabel sizeThatFits:CGSizeMake(fW, fH)];
        if (leftLabelSize.width > fW) {
            leftLabelSize.width = fW;
        }
        leftValueLabel.frame = (CGRect){
            .origin.x = fLeftX,
            .origin.y = (fH - leftLabelSize.height) * .5f,
            .size = leftLabelSize
        };
        
        fLeftX = CGRectGetMaxX(leftValueLabel.frame) + IBT_CELL_INNER_GAP;
    }
    
    // Badge
    IBTBadgeView *badgeView = nil;
    NSString *nsBadgeStr = [cellInfo getUserInfoValueForKey:CInfoBadgeKey];
    BOOL bBadgeRight = [[cellInfo getUserInfoValueForKey:CInfoBadgeAlignmentRightKey] boolValue];
    
    // Set LeftBadge
    if (nsBadgeStr && !bBadgeRight) {
        badgeView =
        [[IBTBadgeView alloc] initWithFrame:(CGRect){
            .origin.x = fLeftX,
            .origin.y = (fH - DEFUALT_BADGE_HEIGHT) * .5f,
            .size.width = DEFUALT_BADGE_HEIGHT,
            .size.height = DEFUALT_BADGE_HEIGHT
        }];
        badgeView.autoresizingMask =
        UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        
        UIColor *badgeColor = [cellInfo getUserInfoValueForKey:CInfoBadgeBGColorKey];
        if ([badgeColor isKindOfClass:[UIColor class]]) {
            [badgeView setBadgeColor:badgeColor];
        }
        
        [cell.contentView addSubview:badgeView];
        
        [badgeView setString:nsBadgeStr];
        
        fLeftX = CGRectGetMaxX(badgeView.frame) + IBT_CELL_INNER_GAP;
    }
    
    // Right Value
    IBTUILabel *rightValueLabel = nil;
    NSString *nsRightValue = [cellInfo getUserInfoValueForKey:CInfoRightValueKey];
    if (nsRightValue.length > 0) {
        rightValueLabel = [[IBTUILabel alloc] init];
        rightValueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        rightValueLabel.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:rightValueLabel];
        
        id rightValueFont = [cellInfo getUserInfoValueForKey:CInfoRightValueFontKey];
        if ([rightValueFont isKindOfClass:[UIFont class]]) {
            rightValueLabel.font = rightValueFont;
        }
        else {
            id rightValueFontSize = [cellInfo getUserInfoValueForKey:CInfoRightValueFontSizeKey];
            CGFloat fRightValueFontSize =
            ([rightValueFontSize isKindOfClass:[NSNumber class]]) ?
            [rightValueFontSize floatValue] :
            IBT_CELL_RIGHT_FONT_SIZE_DEFAULT;
            rightValueLabel.font = [UIFont systemFontOfSize:fRightValueFontSize];
        }
        
        id rightValueColor = [cellInfo getUserInfoValueForKey:CInfoRightValueColorKey];
        if ([rightValueColor isKindOfClass:[UIColor class]]) {
            rightValueLabel.textColor = rightValueColor;
        }
        else {
            rightValueLabel.textColor = IBT_CELL_RIGHT_COLOR_DEFAULT;
        }
        
        rightValueLabel.text = nsRightValue;
        
        fW = MAX(fMaxWidth - fLeftX, IBT_CELL_RIGHT_LABEL_MIN_WIDTH);
        CGSize rightLabelSize = [rightValueLabel sizeThatFits:CGSizeMake(fW, fH)];
        if (rightLabelSize.width > fW) {
            rightLabelSize.width = fW;
            rightValueLabel.autoresizingMask |= UIViewAutoresizingFlexibleWidth;
        }
        rightValueLabel.frame = (CGRect){
            .origin.x = fMaxWidth - rightLabelSize.width,
            .origin.y = (fH - rightLabelSize.height) * .5f,
            .size = rightLabelSize
        };
        
        fRightX = CGRectGetMinX(rightValueLabel.frame) - IBT_CELL_INNER_GAP;
    }
    
    // Set RightBadge
    if (nsBadgeStr && bBadgeRight) {
        badgeView =
        [[IBTBadgeView alloc] initWithFrame:(CGRect){
            .origin.x = fRightX - DEFUALT_BADGE_HEIGHT,
            .origin.y = (CGRectGetHeight(cell.contentView.bounds) - DEFUALT_BADGE_HEIGHT) * .5f,
            .size.width = DEFUALT_BADGE_HEIGHT,
            .size.height = DEFUALT_BADGE_HEIGHT
        }];
        badgeView.bRightAlignment = YES;
        badgeView.autoresizingMask =
        UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        
        UIColor *badgeColor = [cellInfo getUserInfoValueForKey:CInfoBadgeBGColorKey];
        if ([badgeColor isKindOfClass:[UIColor class]]) {
            [badgeView setBadgeColor:badgeColor];
        }
        
        [cell.contentView addSubview:badgeView];
        
        [badgeView setString:nsBadgeStr];
        
        fRightX = CGRectGetMinX(badgeView.frame) - IBT_CELL_INNER_GAP;
    }
}

- (void)makeSwitchCell:(IBTTableViewCellInfo *)cellInfo {
    IBTTableViewCell *cell = cellInfo.cell;
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UISwitch *mSwitch = [self getUserInfoValueForKey:CInfoSwitchKey];
    if ([mSwitch isKindOfClass:[UISwitch class]]) {
        mSwitch.on = [[self getUserInfoValueForKey:CInfoSwitchOnKey] boolValue];
        //        NSLog(@"%@ %@", mSwitch.allTargets, [mSwitch actionsForTarget:mSwitch forControlEvent:UIControlEventValueChanged]);
        cell.accessoryView = mSwitch;
    }
    
    //    cell.editingStyle = cellInfo.editStyle;
    cell.selectionStyle = cellInfo.selectionStyle;
    //    cell.accessoryType = cellInfo.accessoryType;
    
    NSString *nsTitle = [cellInfo getUserInfoValueForKey:CInfoTitleKey];
    id titleFont = [cellInfo getUserInfoValueForKey:CInfoTitleFontKey];
    if ([titleFont isKindOfClass:[UIFont class]]) {
        cell.textLabel.font = titleFont;
    }
    else {
        id titleFontSize = [cellInfo getUserInfoValueForKey:CInfoTitleFontSizeKey];
        CGFloat fTitleFontSize =
        ([titleFontSize isKindOfClass:[NSNumber class]]) ?
        [titleFontSize floatValue] :
        IBT_CELL_TITLE_FONT_SIZE_DEFAULT;
        cell.textLabel.font = [UIFont systemFontOfSize:fTitleFontSize];
    }
    
    id titleColor = [cellInfo getUserInfoValueForKey:CInfoTitleColorKey];
    if ([titleColor isKindOfClass:[UIColor class]]) {
        cell.textLabel.textColor = titleColor;
    }
    else {
        cell.textLabel.textColor = IBT_CELL_TITLE_COLOR_DEFAULT;
    }
    
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.text = nsTitle;
}

- (void)makeCenterCell:(IBTTableViewCellInfo *)cellInfo {
    IBTTableViewCell *cell = cellInfo.cell;
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //    cell.editingStyle = self.editStyle;
    cell.selectionStyle = cellInfo.selectionStyle;
    cell.accessoryType = cellInfo.accessoryType;
    
    NSString *nsTitle = [cellInfo getUserInfoValueForKey:CInfoTitleKey];
    id titleFont = [cellInfo getUserInfoValueForKey:CInfoTitleFontKey];
    if ([titleFont isKindOfClass:[UIFont class]]) {
        cell.textLabel.font = titleFont;
    }
    else {
        id titleFontSize = [cellInfo getUserInfoValueForKey:CInfoTitleFontSizeKey];
        CGFloat fTitleFontSize =
        ([titleFontSize isKindOfClass:[NSNumber class]]) ?
        [titleFontSize floatValue] :
        IBT_CELL_TITLE_FONT_SIZE_DEFAULT;
        cell.textLabel.font = [UIFont systemFontOfSize:fTitleFontSize];
    }
    
    id titleColor = [cellInfo getUserInfoValueForKey:CInfoTitleColorKey];
    if ([titleColor isKindOfClass:[UIColor class]]) {
        cell.textLabel.textColor = titleColor;
    }
    else {
        cell.textLabel.textColor = IBT_CELL_TITLE_COLOR_DEFAULT;
    }
    
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = nsTitle;
}

- (void)makeEditorCell:(IBTTableViewCellInfo *)cellInfo {
    IBTTableViewCell *cell = cellInfo.cell;
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //    cell.editingStyle = self.editStyle;
    cell.selectionStyle = cellInfo.selectionStyle;
    cell.accessoryType = cellInfo.accessoryType;
    
    CGFloat fGap = IBT_CELL_MARGIN;
    CGFloat fLeftX = fGap;
    CGFloat fMaxWidth = CGRectGetWidth(cell.contentView.bounds) - ((cell.accessoryType !=UITableViewCellAccessoryNone || cell.accessoryView) ? 0 : fGap);
    CGFloat fW = fMaxWidth - fLeftX;
    CGFloat fH = self.fCellHeight;
    
    NSString *nsTitle = [cellInfo getUserInfoValueForKey:CInfoTitleKey];
    
    if (nsTitle) {
        id titleFont = [cellInfo getUserInfoValueForKey:CInfoTitleFontKey];
        if ([titleFont isKindOfClass:[UIFont class]]) {
            cell.textLabel.font = titleFont;
        }
        else {
            id titleFontSize = [cellInfo getUserInfoValueForKey:CInfoTitleFontSizeKey];
            CGFloat fTitleFontSize =
            ([titleFontSize isKindOfClass:[NSNumber class]]) ?
            [titleFontSize floatValue] :
            IBT_CELL_TITLE_FONT_SIZE_DEFAULT;
            cell.textLabel.font = [UIFont systemFontOfSize:fTitleFontSize];
        }
        
        id titleColor = [cellInfo getUserInfoValueForKey:CInfoTitleColorKey];
        if ([titleColor isKindOfClass:[UIColor class]]) {
            cell.textLabel.textColor = titleColor;
        }
        else {
            cell.textLabel.textColor = IBT_CELL_TITLE_COLOR_DEFAULT;
        }
        
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.text = nsTitle;
        
        
        CGSize titleLabelSize = [cell.textLabel sizeThatFits:CGSizeMake(fW, fH)];
        if (titleLabelSize.width > fW) {
            titleLabelSize.width = fW;
        }
        CGRect rect = cell.textLabel.frame;
        rect.size.width = titleLabelSize.width;
        cell.textLabel.frame = rect;
        
        
        fGap = IBT_CELL_INNER_GAP;
        id leftMargin = [cellInfo getUserInfoValueForKey:CInfoEditorLMarginKey];
        if (leftMargin) {
            fGap += [leftMargin floatValue];
        }
        
        fLeftX += CGRectGetWidth(cell.textLabel.frame) + fGap;
    }
    else {
        cell.textLabel.text = nsTitle;
    }
    
    
    UITextField *textField = [cellInfo getUserInfoValueForKey:CInfoEditorKey];
    if ([textField isKindOfClass:[UITextField class]]) {
        
        textField.keyboardType = [[cellInfo getUserInfoValueForKey:CInfoEditorKeyboardTypeKey] integerValue];
        textField.secureTextEntry = [[cellInfo getUserInfoValueForKey:CInfoEditorSecureTextEntryKey] boolValue];
        textField.placeholder = [cellInfo getUserInfoValueForKey:CInfoEditorTipKey];
        textField.autocorrectionType = cellInfo.autoCorrectionType;
        textField.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
        textField.textAlignment = [[cellInfo getUserInfoValueForKey:CInfoEditorAlignKey] integerValue];
        
        NSString *nsText = [cellInfo getUserInfoValueForKey:CInfoEditorTextKey];
        if (nsText) {
            textField.text = nsText;
        }
        
        fW = fMaxWidth - fLeftX;
        
        textField.frame = (CGRect){
            .origin.x = fLeftX,
            .origin.y = (CGRectGetHeight(cell.contentView.bounds) - IBT_CELL_TEXTFIELD_DEFAULT_HEIGHT) * .5f,
            .size.width = MAX(IBT_CELL_TEXTFIELD_MIN_WIDTH, fW),
            .size.height = IBT_CELL_TEXTFIELD_DEFAULT_HEIGHT
        };
        
        [cell.contentView addSubview:textField];
        
        if ([[cellInfo getUserInfoValueForKey:CInfoEditorFocusKey] boolValue]) {
            [textField becomeFirstResponder];
        }
    }
}

#pragma mark - Cell Action
- (void)actionSwitchCell:(UISwitch *)sender {
    
    [self addUserInfoValue:@( sender.isOn ) forKey:CInfoSwitchOnKey];
    
    if (self.actionTargetForSwitchCell &&
        [self.actionTargetForSwitchCell respondsToSelector:self.actionSel]) {
        IMP imp = [self.actionTargetForSwitchCell methodForSelector:self.actionSel];
        void (*func)(id, SEL, id) = (void *)imp;
        func(self.actionTargetForSwitchCell, self.actionSel, sender);
    }
}

- (void)actionEditorCell:(NSNotification *)sender {
    
    id <UITextInput> textHolder = sender.object;
    
    if (self.actionTarget &&
        [self.actionTarget respondsToSelector:self.actionSel])
    {
        IMP imp = [self.actionTarget methodForSelector:self.actionSel];
        void (*func)(id, SEL, id) = (void *)imp;
        func(self.actionTarget, self.actionSel, self);
    }
    else {
        NSString *toBeString = [textHolder performSelector:@selector(text)];
        [self addUserInfoValue:toBeString forKey:CInfoEditorTextKey];
    }
    
}

- (void)actionUrlInnerCell {
    NSURL *url = [self getUserInfoValueForKey:CInfoURLKey];
    if ([url isKindOfClass:[NSString class]]) {
        url = [NSURL URLWithString:(id)url];
    }
    
    if ([url isKindOfClass:[NSURL class]] &&
        [[UIApplication sharedApplication] canOpenURL:url])
    {
        // TODO Call your WebView
    }
}

- (void)actionUrlCell {
    NSURL *url = [self getUserInfoValueForKey:CInfoURLKey];
    if ([url isKindOfClass:[NSString class]]) {
        url = [NSURL URLWithString:(id)url];
    }
    
    if ([url isKindOfClass:[NSURL class]] &&
        [[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication] openURL:url];
    }
}

@end


/*
 @{"title":"Friend Radar","titleFont":#'<UICTFont: 0x17df36f0> font-family: ".HelveticaNeueInterface-Regular"; font-weight: normal; font-style: normal; font-size: 16.00pt',"imageName":"add_friend_icon_reda","detail":"Quickly add friends in your vicinity"}
 */
NSString * const CInfoTitleKey                  = @"title";
NSString * const CInfoTitleFontKey              = @"titleFont";
NSString * const CInfoTitleFontSizeKey          = @"titleFontSize";
NSString * const CInfoTitleColorKey             = @"titleColor";
NSString * const CInfoDetailKey                 = @"detail";
NSString * const CInfoDetailFontKey             = @"detailFont";
NSString * const CInfoDetailFontSizeKey         = @"detailFontSize";
NSString * const CInfoDetailColorKey            = @"detailColor";
NSString * const CInfoImageNameKey              = @"imageName";

/*
 @{"imageName":"MoreMyBankCard.png","title":"Wallet","badge":"New"}
 */
NSString * const CInfoBadgeKey                  = @"badge";
NSString * const CInfoBadgeBGColorKey           = @"badgeColor";
NSString * const CInfoBadgeAlignmentRightKey    = @"badgeRight";

/*
 @{"title":"Settings","rightValueFontSize":"14","imageName":"MoreSetting.png","rightValue":"Unprotected"}
 */
NSString * const CInfoRightValueKey             = @"rightValue";
NSString * const CInfoRightValueFontKey         = @"rightValueFont";
NSString * const CInfoRightValueFontSizeKey     = @"rightValueFontSize";
NSString * const CInfoRightValueColorKey        = @"rightValueColor";

/*
 @{"title":"Xummer0","leftValueColor":#"UIDeviceRGBColorSpace 0.341176 0.419608 0.584314 1","leftValue":"2333","url":"http://xummer26.com"}
 */
NSString * const CInfoLeftValueKey              = @"leftValue";
NSString * const CInfoLeftValueFontKey          = @"leftValueFont";
NSString * const CInfoLeftValueFontSizeKey      = @"leftValueFontSize";
NSString * const CInfoLeftValueColorKey         = @"leftValueColor";

NSString * const CInfoURLKey                    = @"url";
NSString * const CInfoSwipeAbleKey              = @"swipeable";

/*
 @{"switch":#"<UISwitch: 0x17ea71d0; frame = (254 6; 51 31); layer = <CALayer: 0x17ea7260>>","title":"Sticky on Top","on":false}
 */
NSString * const CInfoSwitchKey                 = @"switch";
NSString * const CInfoSwitchOnKey               = @"on";

/*
 @{"title":"WeChat ID","fEditorLMargin":0,"focus":true,"keyboardType":1,"editor":#"<UITextField: 0x180a6a50; frame = (108 7; 202 30); text = ''; clipsToBounds = YES; opaque = NO; autoresize = W; gestureRecognizers = <NSArray: 0x178e13a0>; layer = <CALayer: 0x178dc930>>","tip":" "}
 */
NSString * const CInfoEditorKey                 = @"editor";
NSString * const CInfoEditorLMarginKey          = @"fEditorLMargin";
NSString * const CInfoEditorFocusKey            = @"focus";
NSString * const CInfoEditorKeyboardTypeKey     = @"keyboardType";
NSString * const CInfoEditorSecureTextEntryKey  = @"secureTextEntry";
NSString * const CInfoEditorTextKey             = @"text";
NSString * const CInfoEditorTipKey              = @"tip";
NSString * const CInfoEditorAlignKey            = @"textAlign";