2013-07-27 59 views
-4

我重新更新完全我的問題與所有細節更多理解!如何以編程方式聲明並顯示我的UITableView?

因爲他們說圖片不僅僅是文字,我還讓你做了一個小樣本來詳細解釋一切!

首先,我在文中解釋。

我有這樣的:

  • 1 MasterViewController =(RootViewController的)
  • 1的firstView = UITableView中(將顯示與數據許多定製單元)
  • 2 SecondView = UIView的
  • 3 ThirdView = UIView

現在圖片/樣機:

my mockup for my problem

在我MasterViewController.h(代碼一部分)

@property (strong, nonatomic) FirstView *firstView; 
@property (strong, nonatomic) SecondView *secondView; 
@property (strong, nonatomic) ThirdView *thirdView; 

在我MasterViewController.m(代碼一部分)

 firstView = [[LastSalesView alloc] initWithFrame:CGRectMake(0.0, 0.0, result.width, result.height)]; 
     firstView.backgroundColor = [UIColor clearColor]; 
     firstView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     [bottomContainerView addSubview:firstView]; 
     [firstView setHidden:NO]; 

     secondView = [[LastSalesView alloc] initWithFrame:CGRectMake(0.0, 0.0, result.width, result.height)]; 
     secondView.backgroundColor = [UIColor clearColor]; 
     secondView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     [bottomContainerView addSubview:secondView]; 
     [secondView setHidden:NO]; 


     thirdView = [[LastSalesView alloc] initWithFrame:CGRectMake(0.0, 0.0, result.width, result.height)]; 
     thirdView.backgroundColor = [UIColor clearColor]; 
     thirdView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     [bottomContainerView addSubview:thirdView]; 
     [thirdView setHidden:NO]; 



-(void)viewFirstPage:(id)sender { 
    NSLog(@"button first pushed"); 
    [firstView setHidden:NO]; 
    [secondView setHidden:YES]; 
    [thirdView setHidden:YES]; 
} 

-(void)viewSecondPage:(id)sender { 
    NSLog(@"button second pushed"); 
    [firstView setHidden:YES]; 
    [secondView setHidden:NO]; 
    [thirdView setHidden:YES]; 
} 

-(void)viewThirdPage:(id)sender { 
    NSLog(@"button third pushed"); 
    [firstView setHidden:YES]; 
    [secondView setHidden:YES]; 
    [thirdView setHidden:NO]; 
} 
+0

你爲什麼子類的UITableView?我認爲沒有必要這樣做。 – BergQuester

+0

我是新手,我不明白你的問題的含義!我傾向於回答我需要我創建我的UITableView,對吧? – GilbertOOl

+0

啊,知道了。你的代碼肯定反映了一個新手。這裏有太多的錯誤來真正理順它。沒關係,它是一個新手。隨着時間的推移,我相信你會學習。我建議你通過這個[UITableView教程]工作(http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/)。 – BergQuester

回答

0

如果您查看的UITableView然後u可以使用這樣的使用---

MyFirstView.h

#import <UIKit/UIKit.h> 

@class UIViewController; 

@interface MyFirstView : UITableView<UITableViewDelegate,UITableViewDataSource> 

@end 

MyFirstView.m

#import "MyFirstView.h" 

@implementation MyFirstView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.delegate=self; 
     self.dataSource=self; 
     // Initialization code 
    } 
    return self; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 10; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *mycell=[tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"]; 

    if (mycell==nil) { 
     mycell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"]; 
    } 

    [email protected]"My super view is tableView."; 
    return mycell; 
} 

@end 

使用這些的UITableView檔案爲

MyFirstView *tblView=[[MyFirstView alloc] init]; 
[tblView setFrame:CGRectMake(0, 0, 320, 300)]; 

[self.view addSubview:tblView]; 

如果還有不明白的read this

+0

許多很多很多謝謝你的迴應!請檢查,我完全重新更新我的問題與更多細節! – GilbertOOl

+0

是的,我會在瞬間完成,但一個精度,FirstView不是一個控制器,只是一個簡單的UIView! – GilbertOOl

+0

如果是UIView,那麼只需添加@Class UIViewController;在頭文件中。 – Warewolf

1

UITableViewDataSource方法你問一般都屬於視圖控制器。事實上,你的MasterViewController類已經聲明它實現了UITableViewDataSource協議:

@interface MasterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

所以,當你在MasterViewController創建表視圖,視圖控制器需要告訴表視圖,它是數據源(和代表):

myTableView = [[MyTableView alloc] initWithFrame:CGRectMake(0.0, 0.0, result.width, result.height)]; 
myTableView.dataSource = self; 
myTableView.delegate = self; 

這應該讓你在正確的道路上。

+0

好的謝謝,可以直接在我的MyTableView.h和MyTableView.m中執行它? – GilbertOOl

+1

你可以,但你不應該。它違背了MVC模式,因爲它意味着視圖知道如何檢索數據,這是控制器類類的任務。如果你想保持你的代碼整潔,我可以給你一個建議,但它可能太長,評論它 – Can

+0

好的謝謝!我用我已經實現並運行的新代碼更新我的帖子,但正如你所說的代碼一定是壞的我想! – GilbertOOl

1

免責聲明給出這個答案OP更新赴任之前,原來的問題是相當多「我怎麼使用表格視圖?」,現在它已經演變成完全不同的東西。我的答案對某個人來說仍然很有價值。

讓我簡單地向您解釋應該如何使用表視圖。這裏有三個主要部分組成:

  1. UITableView是MVC模式的視圖,其唯一的工作就是做一些數據,並願意這樣做相對於其內容的偏移量,並通過具有一組這樣做當它們離開屏幕時,它將進入隊列的細胞數量,並在應該顯示時將它們出列。如果你注意,UITableView繼承於UIScrollView,所以它所做的就是通過具有可重複使用的單元系統來擴展滾動機制,從而允許它使用最少量的單元。
  2. UITableViewCell負責表示應用內的單個數據。它也是MVC模式中的視圖的一部分。現在
  3. ,一個UITableView需要從什麼地方得到的數據,有兩種可能,要麼你:
    1. 子類UITableViewController
    2. 有另一個類通過符合UITableViewDataSource填寫數據。

無論哪種選擇,你挑,現在將填充在數據的類將成爲控制器在MVC模式(該模式是完全由你決定,可能是一個簡單的字符串數組,就像表格視圖一樣)。

UITableView需要爲他填充(或創建)單元格。這裏有一個重要的區別:

  • 在iOS 4.x和以前,你必須編寫自己的單元格創建,這有點奇怪,因爲它會違背MVC模式。
  • 在iOS 5中引入了registerNib:forCellReuseIdentifier:registerClass:forCellReuseIdentifier:方法,您不再需要創建自己的單元格創建,它會自動檢查是否需要更多單元格,並根據需要實例化它們。

所有的東西都說,如果你只需要改變顯示的數據,就不應該爲表視圖創建子類。

要回答你的問題......

使用筆尖

這完全取決於你,你覺得應該是代表誰,你甚至可以有一個單獨的對象控制的tableview內的數據。

但現在,讓我們運行在MasterViewController。在你的MasterViewController xib文件中,有一個普通的UITableView而沒有子類。確保MasterViewController符合<UITableViewDataSource>,然後將表格視圖dataSource插座連接到MasterViewController(文件所有者最有可能)。

唯一重要的兩個方法是-tableView:cellForRowAtIndexPath:-tableView:numberOfRowsInSection:,執行那些,你的tableView將工作。

其他的事情,如身高,編輯和其他一切,是UITableViewDelegate協議的一部分,如果你關心這些,重複上述步驟,但delegate插座。

通過代碼

我不明白爲什麼人們恨筆尖這麼多,但..什麼,讓我們把它的MasterViewController運行。

MasterViewController.m

#include "MasterViewController.h" 
#include "MyCell.h" 

@interface MasterViewController <UITableViewDataSource, UITableViewDelegate> 
@property (nonatomic,weak) UITableView *tableView; 
@end 

@implementation MasterViewController 

- (void)viewDidLoad 
{ 
    // Notice that this method only gets called if you're using a nib 
    // If you plan of getting rid of nibs ENTIRELY, use -loadView 
    [super viewDidLoad]; 

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 
    tableView.dataSource = self; 
    tableView.delegate = self; 
    [self.view addSubview:tableView]; 

    // Save a reference to it 
    self.tableView = tableView; 

    // iOS 5+ 
    [tableView registerClass:[MyCell class] forCellReuseIdentifier:@"cell.something"]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 10; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Again, iOS 5+ 
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell.something" forIndexPath:indexPath]; 

    cell.textLabel.text = @"Hello world!";   

    return cell; 
} 

@end 

提示覆雜的觀點

此外,我的印象是,你不希望MasterViewController處理有關的數據的代碼表視圖。既然它是一個代表,你可以將它指向任何你想要的!掉落的NSObject符合提到的協議,你可以簡單地這樣做:

Hot IBOutlet action

,如果你正在處理一個非常複雜的視圖,並且具有所有這些額外的tableView:didFart:andSmelledNice:代碼非常有用的礙事。你顯然是通過代碼來做到這一點,但我不會這麼說,認爲這是對遠離筆尖的懲罰。

+0

Waouhhhhhh,這是我的迴應!很多人非常感謝回覆!請檢查,我完全重新更新我的問題與更多細節! – GilbertOOl

+1

@吉爾伯特吉爾伯特,這是你第三次更新你的問題,並且每次你問一些不同的東西。SO的部分理念是,當一個問題得到解答時,你解決它,關閉它,然後,如果你有其他疑問,請發表另外一個問題。這個問題已經發生了很大的變化,現在沒有任何答案是有意義的。您的原始問題似乎針對如何製作UITableViews,現在您希望我們構建特定的佈局。 – Can

+0

不,對不起,問題依然存在,它只涉及UITableView離開! 唯一的變化,我忘了指定UITableView沒有填滿所有的屏幕,但這部分代碼是我的問題! – GilbertOOl

相關問題