IOS用戶端Coding項目記錄(二),ios用戶端coding項目
9:第三方外掛程式整理
JSON轉實體:jsonModel https://github.com/icanzilb/JSONModel/美化按鍵:BButton https://github.com/mattlawer/BButton狀態列提示:JDStatusBarNotification https://github.com/jaydee3/JDStatusBarNotification照片顯示外掛程式:MJPhotoBrowser https://github.com/azxfire/MJPhotoBrowser在列表行划動顯示更多選項:SWTableViewCell https://github.com/cewendel/swtableviewcell圖片查看外掛程式:mwphotobrowser https://github.com/mwaterfall/mwphotobrowser捲軸外掛程式:asprogresspopupview https://github.com/alskipp/asprogresspopupview時間處理:nsdate-helper https://github.com/billymeltdown/nsdate-helper各種進度條:m13progresssuite https://github.com/marxon13/m13progresssuite彈出提示mbprogresshud: https://github.com/jdg/mbprogresshud
10:button顯示設定不同字型
[_headerFansCountBtn setAttributedTitle:[self getStringWithTitle:@"粉絲" andValue:_curUser.fans_count.stringValue] forState:UIControlStateNormal];方法(根據長度來設定其不同的樣式顯示):- (NSMutableAttributedString*)getStringWithTitle:(NSString *)title andValue:(NSString *)value{ NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@", title, value]]; [attriString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName : [UIColor blackColor]} range:NSMakeRange(0, title.length)]; [attriString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName : [UIColor colorWithHexString:@"0x3bbd79"]} range:NSMakeRange(title.length+1, value.length)]; return attriString;}
11:UITableviewcell的accessoryType屬性
cell.accessoryType = UITableViewCellAccessoryNone;//cell沒有任何的樣式 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右邊有一個小箭頭,距離右邊有十幾像素;cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右邊有一個藍色的圓形button; cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右邊的形狀是對號;
12:layoutSubviews在以下情況下會被調用
a、init初始化不會觸發layoutSubviews。b、addSubview會觸發layoutSubviews。c、設定view的Frame會觸發layoutSubviews,當然前提是frame的值設定前後發生了變化。d、滾動一個UIScrollView會觸發layoutSubviews。e、旋轉Screen會觸發父UIView上的layoutSubviews事件。f、改變一個UIView大小的時候也會觸發父UIView上的layoutSubviews事件。g、直接調用setLayoutSubviews。
13:導覽列的按鍵(自訂圖片)
UIButton *settingBtn = [self navButtonWithImageName:@"settingBtn_Nav" action:@selector(settingBtnClicked:)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:settingBtn]; UIButton *addUserBtn = [self navButtonWithImageName:@"addUserBtn_Nav" action:@selector(addUserBtnClicked:)]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addUserBtn];
14:自訂表格格行(繼承UITableViewCell)
@interface TitleRImageMoreCell ()@property (strong, nonatomic) UILabel *titleLabel;@property (strong, nonatomic) UIImageView *userIconView;@end@implementation TitleRImageMoreCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code self.accessoryType = UITableViewCellAccessoryDisclosureIndicator; if (!_titleLabel) { _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, ([TitleRImageMoreCell cellHeight] -30)/2, 100, 30)]; _titleLabel.backgroundColor = [UIColor clearColor]; _titleLabel.font = [UIFont systemFontOfSize:16]; _titleLabel.textColor = [UIColor blackColor]; [self.contentView addSubview:_titleLabel]; } if (!_userIconView) { _userIconView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreen_Width- kTitleRImageMoreCell_HeightIcon)- kPaddingLeftWidth- 30, ([TitleRImageMoreCell cellHeight] -kTitleRImageMoreCell_HeightIcon)/2, kTitleRImageMoreCell_HeightIcon, kTitleRImageMoreCell_HeightIcon)]; [_userIconView doCircleFrame]; [self.contentView addSubview:_userIconView]; } } return self;}- (void)layoutSubviews{ [super layoutSubviews]; if (!_curUser) { return; } self.titleLabel.text = @"頭像"; [self.userIconView sd_setImageWithURL:[_curUser.avatar urlImageWithCodePathResizeToView:_userIconView] placeholderImage:kPlaceholderMonkeyRoundView(_userIconView)];}+ (CGFloat)cellHeight{ return 70.0;}@end然後進行調用時(可以屬性傳值,並在不同的地方載入不同的行及其高度):- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0 && indexPath.row == 0) { TitleRImageMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_TitleRImageMore forIndexPath:indexPath]; cell.curUser = _curUser; [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth]; return cell;}else{TitleValueMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_TitleValueMore forIndexPath:indexPath];}return cell;}
15:當給按鍵的表徵圖進行方向改變
[self setImage:[UIImage imageNamed:@"nav_arrow_down"] forState:UIControlStateNormal];self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, DEGREES_TO_RADIANS(180));//這這是進行180度轉到,向下或向下
16:在導覽列中間增加一個帶圖片文字的控制項
- (UIDownMenuButton *)initWithTitles:(NSArray *)titleList andDefaultIndex:(NSInteger)index andVC:(UIViewController *)viewcontroller{ self = [super init]; if (self) { _titleList = titleList; _curIndex = index; _curShowing = NO; _mySuperView = viewcontroller.view; self.backgroundColor = [UIColor clearColor]; [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; [self.titleLabel setFont:[UIFont systemFontOfSize:kNavTitleFontSize]]; [self.titleLabel setMinimumScaleFactor:0.5]; [self addTarget:self action:@selector(changeShowing) forControlEvents:UIControlEventTouchUpInside]; [self refreshSelfUI]; } return self;}- (void)refreshSelfUI{ NSString *titleStr = @""; DownMenuTitle *menuObj = [self.titleList objectAtIndex:self.curIndex]; titleStr = menuObj.titleValue; CGFloat titleWidth = [titleStr getWidthWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(kScreen_Width, 30)];//獲得文字的寬度 CGFloat btnWidth = titleWidth +kNavImageWidth; self.frame = CGRectMake((kScreen_Width-btnWidth)/2, (44-30)/2, btnWidth, 30); self.titleEdgeInsets = UIEdgeInsetsMake(0, -kNavImageWidth, 0, kNavImageWidth); self.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth, 0, -titleWidth); [self setTitle:titleStr forState:UIControlStateNormal]; [self setImage:[UIImage imageNamed:@"nav_arrow_down"] forState:UIControlStateNormal];}其中幾個值:#define kNavTitleFontSize 19#define kScreen_Width [UIScreen mainScreen].bounds.size.width#define kNavImageWidth (15.0+5.0)然後直接賦給titleview: UIDownMenuButton *navBtn = [[UIDownMenuButton alloc] initWithTitles:titleList andDefaultIndex:index andVC:self]; navBtn.menuIndexChanged = block; self.navigationItem.titleView = navBtn;注意:button都是有帶文字跟圖片的,只是圖片都是在左邊,要通過uiedgeinsetsmake對圖片跟文字進行位置的改變。可以在導覽列的titleview做文章,放一些其它控制項,或視圖;
17:集合視圖UICollectionView的簡單使用
要遵循三個協議:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout設定儲存格的布局UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];[flowLayout setItemSize:CGSizeMake(70, 100)];//設定cell的尺寸[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//設定其布局方向flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//設定其邊界//其布局很有意思,當你的cell設定大小後,一行多少個cell,由cell的寬度決定然後設定集合視圖:_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) collectionViewLayout:flowLayout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [UIColor clearColor]; [_collectionView registerClass:[BMCollectionCell class] forCellWithReuseIdentifier:CELL_ID]; [self.view addSubview:_collectionView];注意:其它委託事件沒全寫出,比如numberOfSectionsInCollectionView等;