IOS开发基础之微博项目
生活随笔
收集整理的這篇文章主要介紹了
IOS开发基础之微博项目
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發基礎之微博項目
關鍵性代碼
// // NJViewController.m // 06-預習-微博(通過代碼自定義cell)// #import "NJViewController.h" #import "NJWeiboCell.h" #import "NJWeibo.h" #import "NJWeiboFrame.h"@interface NJViewController () @property (nonatomic, strong) NSMutableArray *weiboFrames; @end@implementation NJViewController- (NSMutableArray *)weiboFrames {if (_weiboFrames == nil) {// 1.加載數據NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]];// 2.字典 轉成 模型_weiboFrames = [NSMutableArray array];for (NSDictionary *dict in array) {// 創建frame對象NJWeiboFrame *weiboF = [[NJWeiboFrame alloc] init];weiboF.weibo = [NJWeibo weiboWithDict:dict];[_weiboFrames addObject:weiboF];}}return _weiboFrames; }- (BOOL)prefersStatusBarHidden {return YES; }#pragma mark - 數據源方法 #pragma mark 一共有多少行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.weiboFrames.count; }#pragma mark 每一行顯示怎樣的cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {// 1.去緩存池中取出cellstatic NSString *ID = @"weibo";NJWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 2.緩存池沒有cell,重新創建cellif (cell == nil) {cell = [[NJWeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];}// 3.傳遞模型數據cell.weiboFrame = self.weiboFrames[indexPath.row];return cell; }#pragma mark - 代理方法 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return [self.weiboFrames[indexPath.row] cellHeight]; }@end // // NJWeiboCell.h // 06-預習-微博(通過代碼自定義cell) ////#import <UIKit/UIKit.h> @class NJWeiboFrame;@interface NJWeiboCell : UITableViewCell @property (nonatomic, strong) NJWeiboFrame *weiboFrame; @end // // NJWeiboCell.m // 06-預習-微博(通過代碼自定義cell) ////#import "NJWeiboCell.h" #import "NJWeibo.h" #import "NJWeiboFrame.h"@interface NJWeiboCell() // 1.頭像 @property (nonatomic, weak) UIImageView *iconView; // 2.昵稱 @property (nonatomic, weak) UILabel *nameLabel; // 3.會員圖標 @property (nonatomic, weak) UIImageView *vipView; // 4.正文 @property (nonatomic, weak) UILabel *textView; // 5.配圖 @property (nonatomic, weak) UIImageView *pictureView; @end@implementation NJWeiboCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {// 添加內部的子控件// 1.頭像UIImageView *iconView = [[UIImageView alloc] init];[self.contentView addSubview:iconView];self.iconView = iconView;// 2.昵稱UILabel *nameLabel = [[UILabel alloc] init];nameLabel.font = kNameFont;[self.contentView addSubview:nameLabel];self.nameLabel = nameLabel;// 3.會員圖標UIImageView *vipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vip.png"]];[self.contentView addSubview:vipView];self.vipView = vipView;// 4.微博正文UILabel *textView = [[UILabel alloc] init];textView.font = kTextFont;textView.numberOfLines = 0; // 自動換行[self.contentView addSubview:textView];self.textView = textView;// 5.配圖UIImageView *pictureView = [[UIImageView alloc] init];[self.contentView addSubview:pictureView];self.pictureView = pictureView;}return self; }- (void)setWeiboFrame:(NJWeiboFrame *)weiboFrame {_weiboFrame = weiboFrame;// 1.設置微博數據[self settingData];// 2.設置子控件的frame(x、y、width、height)[self settingSubviewFrame]; }#pragma mark 設置子控件的frame - (void)settingSubviewFrame {// 1.頭像self.iconView.frame = self.weiboFrame.iconF;// 2.昵稱self.nameLabel.frame = self.weiboFrame.nameF;// 3.vipself.vipView.frame = self.weiboFrame.vipF;// 4.正文self.textView.frame = self.weiboFrame.textF;// 5.配圖if (self.weiboFrame.weibo.picture) { // 有配圖self.pictureView.frame = self.weiboFrame.pictureF;} }#pragma mark 設置微博數據 - (void)settingData {NJWeibo *weibo = self.weiboFrame.weibo;// 1.頭像self.iconView.image = [UIImage imageNamed:weibo.icon];// 2.昵稱self.nameLabel.text = weibo.name;if (weibo.vip) {self.nameLabel.textColor = [UIColor redColor];} else {self.nameLabel.textColor = [UIColor blackColor];}// 3.會員圖標self.vipView.hidden = !weibo.vip;// 4.正文self.textView.text = weibo.text;// 5.配圖if (weibo.picture) { // 有配圖self.pictureView.hidden = NO;self.pictureView.image = [UIImage imageNamed:weibo.picture];} else { // 沒有配圖self.pictureView.hidden = YES;} } @end // // NJWeibo.h // 06-預習-微博(通過代碼自定義cell) ////#import <Foundation/Foundation.h>@interface NJWeibo : NSObject @property (nonatomic, copy) NSString *text; // 內容 @property (nonatomic, copy) NSString *icon; // 頭像 @property (nonatomic, copy) NSString *name; // 昵稱 @property (nonatomic, copy) NSString *picture; // 配圖 @property (nonatomic, assign) BOOL vip;- (id)initWithDict:(NSDictionary *)dict; + (id)weiboWithDict:(NSDictionary *)dict; @end // // NJWeibo.m // 06-預習-微博(通過代碼自定義cell) // // Created by 李南江 on 14-4-21. // Copyright (c) 2014年 itcast. All rights reserved. //#import "NJWeibo.h"@implementation NJWeibo- (id)initWithDict:(NSDictionary *)dict {if (self = [super init]) {self.name = dict[@"name"];self.vip = [dict[@"vip"] boolValue];self.picture = dict[@"picture"];self.icon = dict[@"icon"];self.text = dict[@"text"];}return self; }+ (id)weiboWithDict:(NSDictionary *)dict {return [[self alloc] initWithDict:dict]; }@end // // NJWeiboFrame.h // 06-預習-微博(通過代碼自定義cell) ////#define kNameFont [UIFont systemFontOfSize:15] #define kTextFont [UIFont systemFontOfSize:15]#import <Foundation/Foundation.h> @class NJWeibo;@interface NJWeiboFrame : NSObject@property (nonatomic, assign, readonly) CGRect iconF; @property (nonatomic, assign, readonly) CGRect nameF; @property (nonatomic, assign, readonly) CGRect vipF; @property (nonatomic, assign, readonly) CGRect textF; @property (nonatomic, assign, readonly) CGRect pictureF;@property (nonatomic, assign, readonly) CGFloat cellHeight;@property (nonatomic, strong) NJWeibo *weibo;@end // // NJWeiboFrame.m // 06-預習-微博(通過代碼自定義cell) // // Created by 李南江 on 14-4-21. // Copyright (c) 2014年 itcast. All rights reserved. //// cell的邊框寬度 #define kCellBorder 10 // 頭像的寬高 #define kIconWH 30 // vip的寬高 #define kVipWH 14 // 圖片尺寸 #define kImageWH 70#import "NJWeiboFrame.h" #import "NJWeibo.h"@implementation NJWeiboFrame - (void)setWeibo:(NJWeibo *)weibo {_weibo = weibo;// 1.頭像CGFloat iconX = kCellBorder;CGFloat iconY = kCellBorder;_iconF = CGRectMake(iconX, iconY, kIconWH, kIconWH);// 2.昵稱// 計算用戶名稱的尺寸CGSize nameSize = [_weibo.name sizeWithFont:kNameFont];CGFloat nameX = CGRectGetMaxX(_iconF) + kCellBorder;CGFloat nameY = iconY + (kIconWH - nameSize.height) * 0.5;_nameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);// 3.vipCGFloat vipX = CGRectGetMaxX(_nameF) + kCellBorder;CGFloat vipY = nameY;_vipF = CGRectMake(vipX, vipY, kVipWH, kVipWH);// 4.正文CGFloat textX = iconX;CGFloat textY = CGRectGetMaxY(_iconF) + kCellBorder;CGFloat textW = 320 - 2 * kCellBorder;// 計算文字尺寸(顯示文字的寬度)CGSize textSize = [_weibo.text sizeWithFont:kTextFont constrainedToSize:CGSizeMake(textW, MAXFLOAT)];_textF = CGRectMake(textX, textY, textW, textSize.height);// 5.配圖\計算cell的高度if (_weibo.picture) { // 有配圖CGFloat pictureX = textX;CGFloat pictureY = CGRectGetMaxY(_textF) + kCellBorder;_pictureF = CGRectMake(pictureX, pictureY, kImageWH, kImageWH);_cellHeight = CGRectGetMaxY(_pictureF) + kCellBorder;} else { // 沒有配圖_cellHeight = CGRectGetMaxY(_textF) + kCellBorder;} } @end總結
以上是生活随笔為你收集整理的IOS开发基础之微博项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux系统C语言学习总结
- 下一篇: 全球通吃的九大黄金专业