IOS開發中tableView顯示列表內容數據(storyboard版)


這是第一次寫博客這類東西,且同為菜鳥級自學IOS,若有哪些不正確的希望您指正,謝謝。。。

 

先寫一個大家自學時都會用到的東西——列表展示,或許您不認為這是問題,那是因為您聰慧,剛學時倒是困擾到我了,特意寫一下;

 

第一步:創建工程IOS--》single view application

     ——》 Product Name:tableViewDemo    

               Language:Objective—C    

               Devices:iPhone,

                                點擊NEXT,選擇您的文件夾,Create

     ——》單擊打開Main.storyboard,  (咱們就用生成項目時自帶的視圖控制器)

              在控件欄中找到UITableView控件,拖拽到試圖控制器上(您可以全部覆蓋,也可以覆蓋一部分區域)

              

 

第二步:設置tableview的dataSource和delegate

        ——》點擊上圖最上面的第一個黃色標志;然后點擊如下圖最上邊的最后一個按鈕;出現如下圖界面;

 

    ——》在上圖的Referencing Outlets一欄,從空心圓圈中拖拽一根線到我們剛剛覆蓋上去的UITableView控件上;彈出dataSource與delegate;選擇delegate;

    ——》重復上個過程的拖拽,並選擇dataSource;

 

第三步:編碼實現顯示

     ——》打開ViewController.h文件

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

@end

 

 

     ——》打開ViewController.m文件,並實現tableview的代理方法

       (1)定義全局變量arrayData,用於裝我們要顯示在列表上的內容文字

       (2)在ViewDidLoad()中,對數組變量進行初始化;

       (3)實現代理方法

復制代碼
#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSArray *arrayData;
@end

@implementation ViewController
@synthesize arrayData;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    arrayData = [NSArray arrayWithObjects:@"王小虎",@"郭二牛",@"宋小六",@"耿老三",@"曹大將軍", nil];
}

#pragma mark -- delegate方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifier];
    }

    cell.textLabel.text = [arrayData objectAtIndex:indexPath.row];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
復制代碼

 

  點擊運行:則可出先如下效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM