IOS開發之TableView、多個TableViewCell、自訂Cell、Cell上畫畫(故事板+代碼方式)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   strong   io   檔案   

最近要做一個項目,有個賬戶設定介面,看了微博、、QQ,他們的帳號設定都比較原生態沒做什麼處理。春雨醫生的帳號不錯,做了許多處理。不說廢話直接上代碼。

第一步:

//UserTableViewCell.h這裡定義第一種Cell#import <UIKit/UIKit.h>@interface UserTableViewCell : UITableViewCell@property (weak, nonatomic) IBOutlet UIImageView *userviewcellicon;@property (weak, nonatomic) IBOutlet UILabel *userviewcellname;@end
//UserTableViewCell2.h這裡定義第二種Cell,#import <UIKit/UIKit.h>@interface UserTableViewCell2 : UITableViewCell@property (weak, nonatomic) IBOutlet UILabel *name;@property (weak, nonatomic) IBOutlet UIImageView *userviewcell2image;@end

故事板裡的TableView和Cell的class和Cell的Identifier就不說了

例:

 

 

2、設計好了這些東西後,開始進TableViewController裡了:

//UserTableViewController.h#import <UIKit/UIKit.h>@interface UserTableViewController : UITableViewController@property(nonatomic,strong) NSDictionary *UserViewCellDic;@property (nonatomic, strong) NSArray *listGroupname;@end
//UserTableViewController.m#import "UserTableViewController.h"#import "UserTableViewCell.h"#import "UserTableViewCell2.h"@interface UserTableViewController ()@end@implementation UserTableViewController- (void)viewDidLoad{    [super viewDidLoad];    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去除分割線    NSBundle *bundle = [NSBundle mainBundle];    NSString *plistpath = [bundle pathForResource:@"UserViewCell" ofType:@"plist"];    self.UserViewCellDic = [[NSDictionary alloc]initWithContentsOfFile:plistpath];    self.listGroupname = [self.UserViewCellDic allKeys];      self.listGroupname = [self.listGroupname sortedArrayUsingSelector:@selector(compare:)];//排序}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView//返回有幾個Section{    return [self.listGroupname count];}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section//Section頭名字設為空白{        NSString *groupName = @" ";    return groupName;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//每個section的行數{        NSString *groupName = [self.listGroupname objectAtIndex:section];    NSArray *listitem = [self.UserViewCellDic objectForKey:groupName];    return [listitem count];    }#pragma mark - TableViewDataSource- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        static NSString *UserViewCellIdentifier = @"UserTableViewCell";    static NSString *UserViewCellIdentifier2 = @"UserTableViewCell2";    NSUInteger section = [indexPath section];    NSUInteger row = [indexPath row];    NSString *groupName = [self.listGroupname objectAtIndex:section];    NSArray *listitem = [self.UserViewCellDic objectForKey:groupName];    NSDictionary *rowDict = [listitem objectAtIndex:row];        if (0 == section) {        UserTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:UserViewCellIdentifier2];        cell.name.text = [rowDict objectForKey:@"name"];        NSString *imagePath = [rowDict objectForKey:@"image"];        imagePath = [imagePath stringByAppendingString:@".png"];        cell.userviewcell2image.image = [UIImage imageNamed:imagePath];        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//最後的箭頭        //畫線        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 79, 320, 1)];        UIView *leftview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 3, 80)];        UIView *rightview = [[UIView alloc]initWithFrame:CGRectMake(317, 0, 3, 80)];        view.backgroundColor = [UIColor colorWithRed:43/255.0f green:43/255.0f blue:233/255.0f alpha:1];        leftview.backgroundColor = [UIColor colorWithRed:43/255.0f green:43/255.0f blue:233/255.0f alpha:1];        rightview.backgroundColor = [UIColor colorWithRed:43/255.0f green:43/255.0f blue:233/255.0f alpha:1];        [cell.contentView addSubview:view];        [cell.contentView addSubview:leftview];        [cell.contentView addSubview:rightview];        return cell;    }else{        UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UserViewCellIdentifier];        cell.userviewcellname.text = [rowDict objectForKey:@"name"];        NSString *imagePath = [rowDict objectForKey:@"image"];        imagePath = [imagePath stringByAppendingString:@".png"];        cell.userviewcellicon.image = [UIImage imageNamed:imagePath];        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//最後的箭頭        //畫線        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 39, 320, 1)];        UIView *leftview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 3, 40)];        UIView *rightview = [[UIView alloc]initWithFrame:CGRectMake(317, 0, 3, 40)];        view.backgroundColor = [UIColor colorWithRed:43/255.0f green:43/255.0f blue:233/255.0f alpha:1];        //alpha是透明度        leftview.backgroundColor = [UIColor colorWithRed:43/255.0f green:43/255.0f blue:233/255.0f alpha:0.3];        rightview.backgroundColor = [UIColor colorWithRed:43/255.0f green:43/255.0f blue:233/255.0f alpha:1];        [cell.contentView addSubview:view];        [cell.contentView addSubview:leftview];        [cell.contentView addSubview:rightview];         return cell;    }          }-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath//Cell的高度{    NSUInteger section = [indexPath section];    if (0 == section) {        return 80;    }else{        return 40;    }   }

這裡附上Plist檔案:

 

 

3、運行效果:

 

寫的不是特別的詳細,如果有什麼不明白的或者需要源碼留郵箱!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.