最近忙完專案比較閒,想寫一篇部落格來分享一些自學iOS的心得體會,希望能對正感到迷茫的你有所幫助。博主並非本科電腦科班出身,一些電腦術語上的不專業之處,還請大家多多指正。我學的是微電子,大四時找了一家深圳的專業對口公司,擔任SMT工程師(也就是戲稱的「殺馬特工程師」),做的是電路板生產,屬於要在車間上班的那種。做了一年多,漸漸感到前途一片迷茫。
2015年3月的時候,我有一個同學來深圳找工作,暫住在我們那裡。後來他報了一個iOS培訓班,我便請他給我一份老師的課件,每天下班後就看幾個小時代碼,遇到不懂的問題就等他晚上回來請教。就這樣,博主從此踏上了iOS之路。博主是個窮小子,筆記型電腦並非蘋果牌,於是四處百度,在Win7下裝了一個虛擬機黑蘋果,好不容易終於能跑Xcode了。晚上下班就一邊看課件一邊敲代碼,最後做出一個簡單的微信介面,心裡還是挺高興的。之後又用一個抓包工具Paros抓取上架App的公共Api來練習獲取網路資料,也能做出簡單的App。
經過幾個月的苦學,自覺已能做出App,於是果斷辭職開始找工作。起初投履歷有回覆的,不是培訓機構就是電話裡當場被拒絕,一度感到相當絕望。但功夫不負有心人,面試了一週後終於有家公司願意栽培新人,於是便入職了。
然而,我的主管在我入職的第四天,就遞給我一張交接單、瀟灑離職高飛了。
原本還想抱個大腿多學點技術,無奈只能硬著頭皮上。那時公司剛好有個智慧手環的專案比較急,真是每天加班加點、狼吞虎嚥地消化新知識,尤其是App第一次上線那晚,一直搞到半夜兩點才下班。
1. 並不是每個人都能像我這麼幸運,遇到一個接觸iOS的機會,在此我要感謝那位同學。但如果你對此感興趣、想入門iOS,只要有C語言基礎就行。博主大學四年都在搞硬件開發,用C語言編程控制單片機,有C語言基礎上手iOS非常快,因為Objective-C是C語言的超集,也就是說它基於C語言。
2. 剛開始時,我對OC裡的一些術語仍不太明白,不要緊。由於忙著做專案,第一個上線的專案算是被「逼」出來的,有些代碼能理解就盡量理解,不能理解的多做幾次自然就懂了,這是我個人的經驗。比如OC裡的物件、實例、類別,一開始老是搞混,現在基本上都理解了。
3. 當你把OC的書看得差不多時,就可以開始你的iOS第一步了。有條件的話買台Mac mini吧,也不貴;沒條件就像博主一樣,在Win7裝個虛擬機黑蘋果。Xcode這套軟體不難,有點英文基礎的人都能上手,一般開發軟體新建工程的步驟都差不多,這裡就不再囉嗦。你可以在網上找一些UI介面的demo(比如微信介面),試著自己敲代碼去搭建;熟悉了一些基本控制項的用法後,就可以用抓包工具Paros,隨意在安卓市場找個簡單的App,抓取它的公共Api,用系統網路請求或AFNetworking方法獲取網路資料,接著試著搭建該App的介面並取得資料,最後大致做出這個App。
以下附上簡單的微信介面demo(代碼是博主從工程複製貼成的txt檔,有些地方如標頭檔顯示不出來,請自行補上,tableViewCell用Xib製作):
AppDelegate.h
// AppDelegate.h
// 自定義UITabBar
#import
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,strong)UITabBarController *tabbarController;
@property(nonatomic,strong)UIView *customTabbarView;
-(void)hidenCustonmTabbarView;//隱藏
-(void)showCustonmTabbarView;//顯示
@end
AppDelegate.m
// AppDelegate.m
// 自定義UITabBar
#import "AppDelegate.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
//隱藏
-(void)hidenCustonmTabbarView
{
[UIView animateWithDuration:0.5 animations:^{
CGRect rect =_customTabbarView.frame;
rect.origin.y=[UIScreen mainScreen].bounds.size.height;
_customTabbarView.frame =rect;
}];
}
//顯示
-(void)showCustonmTabbarView
{
[UIView animateWithDuration:0.5 animations:^{
CGRect rect =_customTabbarView.frame;
rect.origin.y =[UIScreen mainScreen].bounds.size.height-49;
_customTabbarView.frame=rect;
}];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window =[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.backgroundColor =[UIColor whiteColor];
[self.window makeKeyAndVisible];
OneViewController *oneVC =[[OneViewController alloc]init];
TwoViewController *twoVC =[[TwoViewController alloc]init];
ThreeViewController *threeVC=[[ThreeViewController alloc]init];
FourViewController *fourVC=[[FourViewController alloc]init];
UINavigationController *oneNC =[[UINavigationController alloc]initWithRootViewController:oneVC];
UINavigationController *twoNC=[[UINavigationController alloc]initWithRootViewController:twoVC];
UINavigationController *threeNC=[[UINavigationController alloc]initWithRootViewController:threeVC];
UINavigationController *fourNC =[[UINavigationController alloc]initWithRootViewController:fourVC];
_tabbarController =[[UITabBarController alloc]init];
_tabbarController.viewControllers =@[oneNC,twoNC,threeNC,fourNC];
self.window.rootViewController =_tabbarController;
//1、隱藏掉系統的tabbar
_tabbarController.tabBar.hidden=YES;
//2、自定義UIView 替換系統tabbarController.view
_customTabbarView =[[UIView alloc]init];
_customTabbarView.frame=CGRectMake(0, self.window.frame.size.height-49, self.window.frame.size.width, 49);
[_tabbarController.view addSubview:_customTabbarView];
//3、UIImageView
UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _customTabbarView.frame.size.width, _customTabbarView.frame.size.height)];
imageView.image =[UIImage imageNamed:@"tabbarBkg@2x"];
[_customTabbarView addSubview:imageView];
//4、設置View 上的button
int buttonWidth =self.window.frame.size.width/4;
NSArray *imageNames =[NSArray arrayWithObjects:@"tabbar_contacts@2x",@"tabbar_discover@2x",@"tabbar_mainframe@2x",@"tabbar_me@2x", nil];
NSArray *imageHLNames=[NSArray arrayWithObjects:@"tabbar_contactsHL@2x",@"tabbar_discoverHL@2x",@"tabbar_mainframeHL@2x",@"tabbar_meHL@2x", nil];
NSArray *titles=[NSArray arrayWithObjects:@"微信",@"發現",@"聯系人",@"我", nil];
for (int i=0; i<4; i++) {
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame =CGRectMake(buttonWidth*i, 0, buttonWidth, 49);
//圖片
[button setBackgroundImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:imageHLNames[i]] forState:UIControlStateSelected];
//文字
[button setTitle:titles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor cyanColor] forState:UIControlStateSelected];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
//文字偏移量
[button setTitleEdgeInsets:UIEdgeInsetsMake(30, 0, 0, 0)];
button.tag =i+1;
if (i==0) {
button.selected=YES;
}
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[_customTabbarView addSubview:button];
}
// Override point for customization after application launch.
return YES;
}
-(void)buttonClick:(UIButton *)button
{
//視圖切換
_tabbarController.selectedIndex =button.tag-1;
//清除原來的選中狀態
for (int i =0; i<4; i++) {
UIButton * selectbutton =(UIButton *) [_customTabbarView viewWithTag:i+1];
selectbutton.selected=NO;
}
button.selected =YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
OneViewController.m
// OneViewController.m// 自定義UITabBar
#import "OneViewController.h"
#import "NextViewController.h"
#import "AppDelegate.h"
@interface OneViewController (){
NSMutableArray *nameArray;
NSMutableArray *imageArray;
NSMutableArray *textArray;
UITableView *userTable;
NSInteger currentRow;
}
@end
@implementation OneViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[app showCustonmTabbarView];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title =@"微信";
self.navigationController.navigationBar.barStyle =UIBarStyleBlack;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"ShakeforsongBar_ios7@2x"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.translucent =NO;
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"編輯" style:UIBarButtonItemStylePlain target:self action:@selector(compile)];
nameArray =[NSMutableArray array];
imageArray =[NSMutableArray array];
textArray =[NSMutableArray array];
for (int i =0; i<13; i++) {
[nameArray addObject:[NSString stringWithFormat:@"好友%d",i]];
[imageArray addObject:[NSString stringWithFormat:@"test%d",i]];
[textArray addObject:[NSString stringWithFormat:@"我今天在上課 %d",i]];
}
//創建tableView
userTable =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64-49) style:UITableViewStylePlain];
userTable.delegate=self;
userTable.dataSource=self;
userTable.rowHeight=50;
[self.view addSubview:userTable];
}
-(void)compile
{
[userTable setEditing:!userTable.editing animated:YES];//打開編輯模式
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return nameArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier =@"Cell";
//identifier 標識
//采用協議收購一個已經分配的單元,以代替分配一個新的
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell ==nil) {
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
//Subtitle 副標題
}
cell.imageView.image =[UIImage imageNamed:imageArray[indexPath.row]];
cell.textLabel.text =nameArray[indexPath.row];
cell.detailTextLabel.text=textArray[indexPath.row];
return cell;
}
//設置tableView的編輯類型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
//UITableViewCellEditingStyleDelete;
//UITableViewCellEditingStyleInsert;新增
}
//按照設置編輯類型來選擇是刪除還是新增
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//刪除
if (editingStyle ==UITableViewCellEditingStyleDelete) {
//先刪除數據源 再刪除單元格
[nameArray removeObjectAtIndex:indexPath.row];
[imageArray removeObjectAtIndex:indexPath.row];
[textArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}else if (editingStyle ==UITableViewCellEditingStyleInsert)
{
//新增
//先加數據 再新建單元格
[nameArray insertObject:@"我是新增的" atIndex:indexPath.row+1];
[imageArray insertObject:@"test10" atIndex:indexPath.row+1];
[textArray insertObject:@"逗你玩" atIndex:indexPath.row+1];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}
}
//修改刪除模式下刪除按鈕的文字
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [NSString stringWithFormat:@"刪除"];
}
//單元格 移動
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//起點
NSInteger fromRow =sourceIndexPath.row;
//終點
NSInteger toRow =destinationIndexPath.row;
;
//保存數據
NSString *name =nameArray[fromRow];
NSString *imageStr =imageArray[fromRow];
NSString *text =textArray[fromRow];
//刪除數據
[nameArray removeObjectAtIndex:fromRow];
[imageArray removeObjectAtIndex:fromRow];
[textArray removeObjectAtIndex:fromRow];
//插入數據
[nameArray insertObject:name atIndex:toRow];
[imageArray insertObject:imageStr atIndex:toRow];
[textArray insertObject:text atIndex:toRow];
//移動單元格
[tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
//刷新表格
[tableView reloadData];
}
//單元格點擊
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//實例化appdelegate對象
AppDelegate *app =(AppDelegate *)[[UIApplication sharedApplication] delegate];
[app hidenCustonmTabbarView];
NextViewController *next =[[NextViewController alloc]init];
next.name = nameArray[indexPath.row];//正向傳值
next.delegate=self;
currentRow =indexPath.row;
[self.navigationController pushViewController:next animated:YES];
}
-(void)sendUserName:(NSString *)name
{
NSLog(@"name ====%@",name);
[nameArray replaceObjectAtIndex:currentRow withObject:name];
[userTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:currentRow inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
TwoViewController.m
// TwoViewController.m// 自定義UITabBar
#import "TwoViewController.h"
#import "Model.h"
@interface TwoViewController (){
NSMutableArray *dataArray;//tablView數據源
NSMutableArray *filterArry;//索引
NSMutableArray *searchArray;//保存搜索結果
UITableView *useTableView;
UISearchDisplayController *searchDisplay;
}
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title =@"發現";
self.navigationController.navigationBar.barStyle =UIBarStyleBlack;
self.navigationController.navigationBar.translucent =NO;
[self dataSoure];//數據源
useTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49) style:UITableViewStylePlain];
useTableView.rowHeight =50;
useTableView.delegate =self;
useTableView.dataSource=self;
UISearchBar *searchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
searchBar.delegate=self;
useTableView.tableHeaderView =searchBar;
searchDisplay =[[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
searchDisplay.searchResultsDataSource=self;
searchDisplay.searchResultsDelegate =self;
searchDisplay.searchResultsTableView.rowHeight=120;
[self.view addSubview:useTableView];
}
-(void)dataSoure
{
//MVC 設計模式
//高內聚 低耦合 代碼模塊重用度高
//M 數據源 model V View C Controller
NSMutableArray *aArray =[NSMutableArray array];
NSMutableArray *bArray =[NSMutableArray array];
NSMutableArray *cArray =[NSMutableArray array];
NSMutableArray *dArray =[NSMutableArray array];
for (int i =0; i<7; i++) {
Model *model =[[Model alloc]init];
model.name =[NSString stringWithFormat:@"a%d",i];
model.imageName =[NSString stringWithFormat:@"test%d",i];
[aArray addObject:model];
}
for (int i =0; i<5; i++) {
Model *model =[[Model alloc]init];
model.name =[NSString stringWithFormat:@"b%d",i];
model.imageName =[NSString stringWithFormat:@"test%d",i];
[bArray addObject:model];
}
for (int i =0; i<8; i++) {
Model *model =[[Model alloc]init];
model.name =[NSString stringWithFormat:@"c%d",i];
model.imageName =[NSString stringWithFormat:@"test%d",i];
[cArray addObject:model];
}
for (int i =0; i<4; i++) {
Model *model =[[Model alloc]init];
model.name =[NSString stringWithFormat:@"c%d",i];
model.imageName =[NSString stringWithFormat:@"test%d",i];
[dArray addObject:model];
}
dataArray =[NSMutableArray arrayWithObjects:aArray,bArray,cArray,dArray, nil];
filterArry =[NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"D", nil];
searchArray =[NSMutableArray array];
}
#pragma mark -UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView ==useTableView) {
return dataArray.count;
}
else
{
return 1;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView ==useTableView) {
NSArray *arr =dataArray[section];
return arr.count;
}
else
{
return searchArray.count;
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *str =@"Cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
if (cell ==nil) {
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
//在數據源中取出Model
if (tableView ==useTableView) {
NSArray *arr =dataArray[indexPath.section];
Model *model =arr[indexPath.row];
cell.imageView.image =[UIImage imageNamed:model.imageName];
cell.textLabel.text =model.name;
}else
{
Model *model =searchArray[indexPath.row];
cell.textLabel.text =model.name;
cell.imageView.image =[UIImage imageNamed:model.imageName];
}
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return filterArry[section];
}
#pragma mark -UISeachDisPlayDelegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchArray.count>0) {
[searchArray removeAllObjects];
}
for (NSArray *arr in dataArray) {
for (Model *model in arr) {
if ([model.name rangeOfString:searchText].location!=NSNotFound) {
[searchArray addObject:model];
}
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return filterArry;
}
//點擊某一個索引
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
@end
ThreeViewController.m
// ThreeViewController.m// 自定義UITabBar
#import "ThreeViewController.h"
#import "Model.h"
#import "TableViewCell.h"
@interface ThreeViewController (){
NSMutableArray *dataSoureArry;
UITableView *userTable;
}
@end
@implementation ThreeViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barStyle =UIBarStyleBlack;
self.navigationController.navigationBar.translucent=NO;
self.title =@"發現";
[self getData];
userTable =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49) style:UITableViewStyleGrouped];
userTable.rowHeight =70;
userTable.delegate=self;
userTable.dataSource=self;
[self.view addSubview:userTable];
}
-(void)getData
{
Model *model =[[Model alloc]init];
model.headImageName =@"ff_IconShowAlbum_ios7@2x";
model.name =@"朋友圈";
model.imageName =@"test6";
NSArray *aArray =[NSArray arrayWithObject:model];
NSMutableArray *bArray =[NSMutableArray array];
for (int i=0; i<2; i++) {
Model *model=[[Model alloc]init];
if (i==0) {
model.headImageName =@"ff_IconShowAlbum_ios7@2x";
model.name =@"掃一掃";
}
else
{
model.headImageName =@"ff_IconShake_ios7@2x";
model.name =@"搖一搖";
}
[bArray addObject:model];
}
NSMutableArray *cArray =[NSMutableArray array];
for (int i=0; i<2; i++) {
Model *model=[[Model alloc]init];
if (i==0) {
model.headImageName =@"ff_IconLocationService_ios7@2x";
model.name =@"附近的人";
}
else
{
model.headImageName =@"FriendCardNodeIconBottle@2x";
model.name =@"漂流瓶";
}
[cArray addObject:model];
}
NSMutableArray *dArray =[NSMutableArray array];
Model *gameModel =[[Model alloc]init];
gameModel.headImageName =@"MoreGame@2x";
gameModel.name =@"游戲";
[dArray addObject:gameModel];
dataSoureArry =[NSMutableArray arrayWithObjects:aArray,bArray,cArray,dArray, nil];
}
#pragma mark - UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return dataSoureArry.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *arr =dataSoureArry[section];
return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section ==0) {
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell ==nil) {
cell =[[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSArray *arr =[dataSoureArry objectAtIndex:indexPath.section];
Model *model =arr[indexPath.row];
[cell setModel:model];
[cell showRed:YES];
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;//右箭頭
return cell;
}else
{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (cell ==nil) {
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
NSArray *arr =[dataSoureArry objectAtIndex:indexPath.section];
Model *model =arr[indexPath.row];
cell.imageView.image =[UIImage imageNamed:model.headImageName];
cell.textLabel.text =model.name;
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
FourViewController.m
// FourViewController.m// 自定義UITabBar
#import "FourViewController.h"
#import "Model.h"
#import "Cell.h"
#import "TableViewCell.h"
@interface FourViewController (){
NSMutableArray *_dataSoureArray;
UITableView *userTableView;
}
@end
@implementation FourViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"我";
self.navigationController.navigationBar.barStyle =UIBarStyleBlack;
self.navigationController.navigationBar.translucent =NO;
[self getData];//加載數據
userTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49) style:UITableViewStyleGrouped];
userTableView.delegate=self;
userTableView.dataSource =self;
//注冊xib單元格
[userTableView registerNib:[UINib nibWithNibName:@"Cell" bundle:nil] forCellReuseIdentifier:@"oneCell"];//按照XIB文件注冊
userTableView .rowHeight =50;
[self.view addSubview:userTableView];
}
#pragma mark -UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _dataSoureArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *arr =_dataSoureArray[section];
return arr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell =[tableView dequeueReusableCellWithIdentifier:@"oneCell"];
NSArray *arr =_dataSoureArray[indexPath.section];
Model *model =arr[indexPath.row];
cell.headImageView.image =[UIImage imageNamed:model.headImageName];
cell.nameLabel.text =model.name;
cell.friendImageView.image =[UIImage imageNamed:model.imageName];
return cell;
}
-(void)getData
{
Model *model =[[Model alloc]init];
model.headImageName =@"ff_IconShowAlbum_ios7@2x";
model.name =@"朋友圈";
model.imageName =@"test6";
NSArray *aArray =[NSArray arrayWithObject:model];
NSMutableArray *bArray =[NSMutableArray array];
for (int i=0; i<3; i++) {
Model *model=[[Model alloc]init];
if (i==0) {
model.headImageName =@"MoreMyAlbum_ios7@2x";
model.name =@"相冊";
}
else if(i==1)
{
model.headImageName =@"MoreMyFavorites_ios7@2x";
model.name =@"收藏";
}
else{
model.headImageName=@"MoreMyBankCard_ios7@2x";
model.name =@"銀行卡";
}
[bArray addObject:model];
}
NSMutableArray *cArray =[NSMutableArray array];
Model *myModel=[[Model alloc]init];
myModel.headImageName =@"MoreMySafe_ios7@2x";
myModel.name =@"安全";
[cArray addObject:myModel];
NSMutableArray *dArray =[NSMutableArray array];
Model *gameModel =[[Model alloc]init];
gameModel.headImageName =@"MoreSetting_ios7@2x";
gameModel.name =@"設置";
[dArray addObject:gameModel];
_dataSoureArray =[NSMutableArray arrayWithObjects:aArray,bArray,cArray,dArray, nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NextViewController.h
// NextViewController.h// 自定義UITabBar
#import
//聲明協議
@protocol NextViewControllerDelegate
//定義方法
-(void)sendUserName:(NSString*)name;
@end
@interface NextViewController : UIViewController
@property(nonatomic,strong)NSString *name;
//協議對象
@property(nonatomic,weak)iddelegate;
@end
NextViewController.m
// NextViewController.m
// 自定義UITabBar
#import "NextViewController.h"
@interface NextViewController ()
{
UITextField *textField ;
}
@end
@implementation NextViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@" %@",self.name);
textField =[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 30)];
textField.borderStyle=UITextBorderStyleRoundedRect;
textField.text =self.name;
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(Click)];
[self.view addSubview:textField];
}
-(void)Click
{
NSString *str =[NSString stringWithFormat:@"%@",textField.text];
//判斷協議對象和協議方法是不是被響應
if (_delegate&&[_delegate respondsToSelector:@selector(sendUserName:)]) {
[_delegate sendUserName:str];
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Model.h
// Model.h// 自定義UITabBar
#import
@interface Model : NSObject
@property (nonatomic,strong)NSString *imageName;
@property (nonatomic,strong)NSString *name;
@property (nonatomic,strong)NSString *headImageName;
@end
TableViewCell.h
// TableViewCell.h// 自定義UITabBar
#import
#import "Model.h"
@interface TableViewCell : UITableViewCell
@property (nonatomic,strong)UIImageView *headImageView;
@property (nonatomic,strong)UIImageView *fridendImageView;
@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,assign)BOOL isShowRed;
-(void)setModel:(Model*)model;//數據
-(void)showRed:(BOOL)isShowRed;//是否顯示紅點
@end
TableViewCell.m
// TableViewCell.m
// 自定義UITabBar
#import "TableViewCell.h"
@implementation TableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//頭像
_headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 25, 30, 30)];
//名字
_nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(50, 25, 100, 30)];
_nameLabel.font =[UIFont boldSystemFontOfSize:17];
//朋友圈頭像
_fridendImageView =[[UIImageView alloc]initWithFrame:CGRectMake(280, 10, 50, 50)];
[self.contentView addSubview:_fridendImageView];
[self.contentView addSubview:_nameLabel];
[self.contentView addSubview:_headImageView];
}
return self;
}
-(void)setModel:(Model *)model
{
//頭像
_headImageView.image =[UIImage imageNamed:model.headImageName];
_nameLabel.text=model.name;
_fridendImageView.image =[UIImage imageNamed:model.imageName];
}
-(void)showRed:(BOOL)isShowRed
{
//如果是顯示
if (isShowRed) {
int width =20;
UIImageView *redImageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"AlbumNewNotify_ios7@2x"]];
redImageView.frame =CGRectMake(_fridendImageView.frame.size.width-width/2, _fridendImageView.frame.origin.y-width, width, width);
[_fridendImageView addSubview:redImageView];
}
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
4. 當你能基本完成以上幾點時,就可以試著找工作了。如果你是科班出身,找工作會比較容易;不是科班出身也不必驚慌,博主就是學硬件的,一樣能找到。面試時若能答出一些基本問題,只要你態度夠誠懇、薪水方面壓低一些,一般沒什麼問題。先入門、進入一個平台實習,以後薪水會漲得很快。
總結一下:只要你感興趣、努力用心地去學,沒有什麼是做不到的。(以上一些關於iOS實際應用中遇到的問題,博主會逐漸更新,敬請期待!)
越努力,越幸運!