事半功倍的静态TableView(Static TableVie

2017-04-14  本文已影响55人  李乾坤David
Coming Soon

起因

在IOS开发中可能遇到设置页面,个人主页这样静态的TableView。我刚开始接手我们公司项目的时候遇到了这样的代码弄的我很头疼。

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   UITableViewCell *cell;
   if (indexPath.section == 0){
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
       if (indexPath.row == 0) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
           UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 15)];
           title.text = @"基本信息";
           title.font = [UIFont systemFontOfSize:14];
           title.textColor = SummaryColor;
           cell.contentView.backgroundColor = BackColor;
           [title sizeToFit];
           [title setFrame:CGRectMake(appDelegate.rootController.view.frame.size.width/2-title.frame.size.width/2, 15, title.frame.size.width, title.frame.size.height)];
           [cell.contentView addSubview:title];
       }
       else if (indexPath.row == 1) {
           if ([[[User currentUser] objectForKey:@"has_avatar"] boolValue]) {
               NZCircularImageView *avatarView = [[NZCircularImageView alloc] initWithFrame:CGRectMake(7.5, 7.5, 45, 45)];
               [avatarView sd_setImageWithURL:[NSURL URLWithString:[[User currentUser] objectForKey:@"medium_avatar_url"]] placeholderImage:[UIImage imageNamed:@"medium_avatar.png"] options:SDWebImageCacheMemoryOnly];
               cell.accessoryView = avatarView;
               cell.textLabel.text = @"头像";
               cell.textLabel.textColor = SummaryColor;
               cell.textLabel.font = [UIFont systemFontOfSize:14];
           }
           else{
               cell.detailTextLabel.text = @"未设置";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#FF8831"];
               cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
               cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
               cell.textLabel.text = @"头像";
               cell.textLabel.textColor = SummaryColor;
               cell.textLabel.font = [UIFont systemFontOfSize:14];
           }
       }
       else if (indexPath.row == 2) {
           cell.textLabel.text = @"姓名";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           cell.detailTextLabel.text = NULL_TO_Blank([[User currentUser] objectForKey:@"name"]);
           cell.detailTextLabel.textColor = SharingColor;
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 3) {
           cell.textLabel.text = @"手机";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           if ([[[User currentUser] objectForKey:@"phone"] isEqual:[NSNull null]]) {
               cell.detailTextLabel.text = @"未设置";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
               
           }else {
               cell.detailTextLabel.text = [[User currentUser] objectForKey:@"phone"];
               cell.detailTextLabel.textColor = SharingColor;
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 4) {
           cell.textLabel.text = @"电子邮件";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           NSString *email = [NSString stringWithFormat:@"%@",[[User currentUser] objectForKey:@"email"]];
           if (email.length > 0) {
               cell.detailTextLabel.text = [[User currentUser] objectForKey:@"email"];
               cell.detailTextLabel.textColor = SharingColor;
               if ([[[User currentUser] objectForKey:@"pending_reconfirmation"] boolValue]) {
                   UILabel *notice = [[UILabel alloc] initWithFrame:CGRectMake(appDelegate.rootController.view.frame.size.width - 160-23, 30, 160, 9)];
                   notice.font = [UIFont systemFontOfSize:9];
                   notice.text = @"(地址未确认,请点击确认邮件中的链接)";
                   notice.textColor = [UIColor colorWithHexString:@"EE4747"];
                   [cell addSubview:notice];
               }
           }else {
               cell.detailTextLabel.text = @"未设置";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 5) {
           NSString *email = [NSString stringWithFormat:@"%@",[[User currentUser] objectForKey:@"email"]];
           if ((![[User currentPhone] isEqual:[NSNull null]]) || ([email length] > 1 && ![[[User currentUser] objectForKey:@"pending_reconfirmation"] boolValue])) {
               cell.textLabel.text = @"密码";
               cell.textLabel.textColor = SummaryColor;
               cell.textLabel.font = [UIFont systemFontOfSize:14];
               if ([[[User currentUser] objectForKey:@"password"] boolValue]) {
                   cell.detailTextLabel.text = @"修改";
                   cell.detailTextLabel.textColor = SharingColor;
                   cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
                   cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

               } else {
                   cell.detailTextLabel.text = @"未设置";
                   cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
                   cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
                   cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
               }
           }
           else {
               
           }
       }
       else if (indexPath.row == 6) {
           cell.textLabel.text = @"性别";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           
           if ([[[User currentUser] objectForKey:@"gender"] intValue] == 0) {
               cell.detailTextLabel.text = @"男";
           }
           else if ([[[User currentUser] objectForKey:@"gender"] intValue] == 1){
               cell.detailTextLabel.text = @"女";
           }
           cell.detailTextLabel.textColor = SharingColor;
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 7) {
           cell.textLabel.text = @"生日";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           NSString *key = [[User currentUser] objectForKey:@"birthday"];
           if ([key isEqual:[NSNull null]]) {
               cell.detailTextLabel.text = @"未填写";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
           }else {
               cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [[User currentUser] objectForKey:@"birthday"]];
               cell.detailTextLabel.textColor = SharingColor;
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 8) {
           cell.textLabel.text = @"所在地";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
         
           if ([[[User currentUser] objectForKey:@"location"] length] < 1) {
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
               cell.detailTextLabel.text = @"未填写";
           }else{
               cell.detailTextLabel.text = [[User currentUser] objectForKey:@"location"];
           }
           cell.detailTextLabel.textColor = SharingColor;
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 9) {
           cell.textLabel.text = @"行业方向";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           if ([[[User currentUser] objectForKey:@"areas"] count] < 1) {
               cell.detailTextLabel.text = @"未填写";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
           }else {
               //cell.detailTextLabel.text = [[[User currentUser] objectForKey:@"areas"] componentsJoinedByString:@","];
               NSString *areasStr = [[User currentUser][@"areas"] firstObject];
               NSString *rolesStr = [[User currentUser][@"roles"] firstObject];
               cell.detailTextLabel.text = [NSString stringWithFormat:@"%@/%@",areasStr,rolesStr];
               cell.detailTextLabel.textColor = SharingColor;
           }
           
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 10) {
           cell.textLabel.text = @"相关领域的经验";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           if ([[[User currentUser] objectForKey:@"experience"] isEqual:[NSNull null]]) {
               cell.detailTextLabel.text = @"未填写";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
               
           }else {
               cell.detailTextLabel.text = [[User currentUser] objectForKey:@"experience"];
               cell.detailTextLabel.textColor = SharingColor;
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 11) {
           cell.textLabel.text = @"一句话个人简介";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           if ([[[User currentUser] objectForKey:@"headline"] isEqual:[NSNull null]] || [[[User currentUser] objectForKey:@"headline"] length] < 1) {
               cell.detailTextLabel.text = @"未填写";
               cell.detailTextLabel.textColor = WorkShopColor;
               
           }else {
               cell.detailTextLabel.text = [[User currentUser] objectForKey:@"headline"];
               cell.detailTextLabel.textColor = SharingColor;
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }

   }
   else if (indexPath.section == 1) {
       if (indexPath.row == 0) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
           UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 15)];
           title.text = @"合伙需求";
           title.font = [UIFont systemFontOfSize:14];
           title.textColor = SummaryColor;
           cell.contentView.backgroundColor = BackColor;
           [title sizeToFit];
           [title setFrame:CGRectMake(appDelegate.rootController.view.frame.size.width/2-title.frame.size.width/2, 15, title.frame.size.width, title.frame.size.height)];
           [cell.contentView addSubview:title];
       }
       else if (indexPath.row == 1) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
           CGFloat height = [Helpers heightForString:[NSString stringWithFormat:@"%@\n", [_user objectForKey:@"bio"]] font:[UIFont systemFontOfSize:12] width:appDelegate.rootController.view.frame.size.width - 20 - 35];
           UITextView *title = [[UITextView alloc] initWithFrame:CGRectMake(20, 5, appDelegate.rootController.view.frame.size.width - 20 - 35, height)];
           title.textColor = SharingColor;
           title.editable = NO;
           title.userInteractionEnabled = NO;
           title.font = [UIFont systemFontOfSize:14];
           if ([[[User currentUser] objectForKey:@"bio"] isEqual:[NSNull null]]) {
               title.text = @"未填写";
               title.textColor = WorkShopColor;
           }else {
               title.text = [[User currentUser] objectForKey:@"bio"];
           }
           [title sizeToFit];
           cell.frame = CGRectMake(0, 0, appDelegate.rootController.view.frame.size.width, 5 + title.frame.size.height);
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
           [cell.contentView addSubview:title];
       }
   }
   else if (indexPath.section == 2){
       if (indexPath.row == 0) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
           UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 15)];
           title.text = @"创业信息";
           cell.contentView.backgroundColor = BackColor;
           title.font = [UIFont systemFontOfSize:14];
           title.textColor = SummaryColor;
           [title sizeToFit];
           [title setFrame:CGRectMake(appDelegate.rootController.view.frame.size.width/2-title.frame.size.width/2, 15, title.frame.size.width, title.frame.size.height)];
           [cell.contentView addSubview:title];
       }
       
       else if (indexPath.row == 1){
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
           cell.textLabel.text = @"创业状态";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           if ([[[User currentUser] objectForKey:@"status"] isEqual:[NSNull null]]) {
               cell.detailTextLabel.text = @"未填写";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
               
           }else {
               cell.detailTextLabel.text = [[User currentUser] objectForKey:@"status"];
               cell.detailTextLabel.textColor = SharingColor;
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 2){
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
           cell.textLabel.text = @"可投入资金";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           if ([[[User currentUser] objectForKey:@"money"] isEqual:[NSNull null]]) {
               cell.detailTextLabel.text = @"未填写";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
               
           }else {
               cell.detailTextLabel.text = [[User currentUser] objectForKey:@"money"];
               cell.detailTextLabel.textColor = SharingColor;
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
       else if (indexPath.row == 3){
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
           cell.textLabel.text = @"可投入时间";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           if ([[[User currentUser] objectForKey:@"time"] isEqual:[NSNull null]]) {
               cell.detailTextLabel.text = @"未填写";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
               
           }else {
               cell.detailTextLabel.text = [[User currentUser] objectForKey:@"time"];
               cell.detailTextLabel.textColor = SharingColor;
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
   }
   else if (indexPath.section == 3){
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
       if (indexPath.row == 0){
           UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 15)];
           title.text = @"工作和教育经历";
           cell.contentView.backgroundColor = BackColor;
           title.font = [UIFont systemFontOfSize:14];
           title.textColor = SummaryColor;
           [title sizeToFit];
           [title setFrame:CGRectMake(appDelegate.rootController.view.frame.size.width/2-title.frame.size.width/2, 15, title.frame.size.width, title.frame.size.height)];
           [cell.contentView addSubview:title];
       }
       else if (indexPath.row == 1){
           cell.textLabel.text = @"工作经历";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           NSArray *workInfo = [[User currentResume] objectForKey:@"professional_experiences"];
           if ([workInfo count] > 1) {
               cell.detailTextLabel.text = [NSString stringWithFormat:@"%@等%lu段工作经历",[[workInfo firstObject] objectForKey:@"company"], (unsigned long)[workInfo count]];
               cell.detailTextLabel.textColor = SharingColor;
           }
           else if ([workInfo count] == 1) {
               cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [[workInfo firstObject] objectForKey:@"company"]];
               cell.detailTextLabel.textColor = SharingColor;
           }else if ([workInfo count] == 0) {
               cell.detailTextLabel.text = @"未设置";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
           
       }
       else if (indexPath.row == 2){
           cell.textLabel.text = @"教育经历";
           cell.textLabel.textColor = SummaryColor;
           cell.textLabel.font = [UIFont systemFontOfSize:14];
           NSArray *eduInfo = [[User currentResume] objectForKey:@"educational_experiences"];
           if ([eduInfo count] > 1) {
               cell.detailTextLabel.text = [NSString stringWithFormat:@"%@等%lu段教育经历",[[eduInfo firstObject] objectForKey:@"school"], (unsigned long)[eduInfo count]];
               cell.detailTextLabel.textColor = SharingColor;
           }
           else if ([eduInfo count] == 1) {
               cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [[eduInfo firstObject] objectForKey:@"school"]];
               cell.detailTextLabel.textColor = SharingColor;
           }else if ([eduInfo count] == 0) {
               cell.detailTextLabel.text = @"未设置";
               cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"00CD48"];
           }
           cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       }
   }
   else if (indexPath.section == 4) {
       if (indexPath.row == 0) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
           cell.detailTextLabel.text = @"修改";
           cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
           cell.detailTextLabel.textColor = WorkShopColor;
           UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 15)];
           title.text = @"技能标签";
           cell.contentView.backgroundColor = BackColor;
           title.font = [UIFont systemFontOfSize:14];
           title.textColor = SummaryColor;
           [title sizeToFit];
           [title setFrame:CGRectMake(appDelegate.rootController.view.frame.size.width/2-title.frame.size.width/2, 15, title.frame.size.width, title.frame.size.height)];
           [cell.contentView addSubview:title];
       }
       else if (indexPath.row == 1) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProfileSettingsCellIdentifier];
           [self addSkillLabelOnCell:cell];
       }
       
   }
   
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
   return cell;

为了让大家一起痛苦我把所有的代码都贴出来了,有一句话怎么说来着,老太太的裹脚布又臭又长。我看了代码打算用静态TableView进行重写。痛苦啊!太痛苦了!

Static TableView的使用

  1. 在StoryBoard中拖入TableViewController


    创建静态TableView
  2. 开始添加设置Cell
上一篇下一篇

猜你喜欢

热点阅读