2011-11-09 64 views
2

創建編程多個表我需要創建iPad程序,其中i有,以顯示多個表在一個單一的視圖(沒有網格,僅1具有多個行collumn)。這必須以編程方式完成,因爲在後臺某人要設置該數量的表。ipad的如何在一個視圖

視圖將有一個滾動這樣我就可以看到它們。

這可以做對嗎?

有人可以提供我一些代碼或鏈接到有關如何在一個視圖中定位他們時,我想創建表的N多一些教程。

回答

2

這完全可以做到的。

也許你可以做到這一點最簡單的方法是子類的UITableView,使每個創建可以有它的委託和數據源,翼獨特的處理程序的TableView:

DynamicTableView.h

@interface DynamicTableView : UITableView <UITableViewDelegate, UITableViewDataSource> { 
    NSMutableArray *items; 
} 

@end 

DynamicTableView.m

#import "DynamicTableView.h" 

@implementation DynamicTableView 

-(id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style { 
    if (self == [super initWithFrame:frame style:style]) { 
     items = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:@"%i", [NSDate timeIntervalSinceReferenceDate]], 
       [NSString stringWithFormat:@"%i", [NSDate timeIntervalSinceReferenceDate]], nil]; 
    } 

    return self; 
} 

-(void) dealloc { 
    [items release]; 

    [super dealloc]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [items count]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [items objectAtIndex:indexPath.row]; 

    return cell; 
} 

@end 

這是一個非常簡單的實現,當它的初始化填充其數據源(項陣列)具有兩個時間戳。使用它是像那樣簡單:

for (int i = 0; i < 4; i++) { 
    DynamicTableView *table = [[[DynamicTableView alloc] initWithFrame:CGRectMake(10, (i * 100) + 10, 200, 50) style:UITableViewStylePlain] autorelease]; 
    [table setDelegate:table]; 
    [table setDataSource:table]; 

    [self.view addSubview:table]; 
} 

修改DynamicTableView接受你想要的任何數據源,以及它是如何顯示的。

希望有幫助!

+0

thx !! idd我需要多個類型的表視圖。我需要視頻,照片,純文本和其他東西。 現在,我可以在一個視圖中創建表格,如果我製作150k表格,如何滾動視圖? 它不是一個好的做法吧?我的問題是,我需要類似的hitpad應用程序。 thx預先 –

+0

你很好,滾動視圖。你需要做的是跟蹤你添加了多少個表格視圖並相應地設置你的內容大小。例如,在初始化表的循環之前,將整數設置爲0.在循環內部,創建每個循環時,將整數設置爲+ = table.frame.origin.y。循環後,設置滾動視圖的內容視圖,使用整數作爲高度的參數! – Geekswordsman

+0

老兄,正是我需要的東西:D只需要創建自定義單元格和即時沙丘與此。 thx thx thx thx thx thx thx !!! –

0

從你的描述,我假設你想有表任意數量的,所有這些都坐在這本身就是滾動(讓你向上或向下滾動來獲得所有的單一視圖表)。這在iOS中是非常不明智的,因爲表視圖本身就是可滾動視圖的子類,並且在使用「主」滾動視圖時單個表可以正確滾動,這會產生嚴重問題。

假設這是你想要做什麼,你會好得多使用被分成部分的單一表視圖。這裏是a good tutorial,顯示如何做到這一點。

+0

爲了更明確一點,我的老闆喜歡hitpad應用程序和他們展示他們的主要觀點的乳清。 任何想法他們如何做到這一點? –

0

我希望下面的代碼可能是你的起點:

@interface MyController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

@property (nonatomic, retain) UITableView *table1; 
@property (nonatomic, retain) UITableView *table2; 

@end 

@implementation MyController 

@synthesize table1 = _table1, 
      table2 = _table2; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    CGRect table1Rect = CGRectMake(0, 0, 200, 300); 
    UITableView *table1 = [[UITableView alloc] initWithFrame:table1Rect style:UITableViewStyleGrouped]; 
    table1.delegate = self; 
    table1.dataSource = self; 
    [self.view addSubview:table1]; 
    self.table1 = table1; 
    [table1 release]; 

    CGRect table2Rect = CGRectMake(200, 0, 200, 300); 
    UITableView *table2 = [[UITableView alloc] initWithFrame:table2Rect style:UITableViewStyleGrouped]; 
    table2.delegate = self; 
    table2.dataSource = self; 
    [self.view addSubview:table2]; 
    self.table2 = table2; 
    [table2 release]; 
} 

- (void)viewDidUnload { 
    self.table1 = nil; 
    self.table2 = nil; 
} 

- (void)dealloc { 
    self.table1 = nil; 
    self.table2 = nil; 
    [super dealloc]; 
} 

@end 
+0

這有幫助,我如何滾動主視圖,我有這2個表? –

+0

有什麼問題呢? – yas375

0

如果你是把可以說在你的viewController和您的委託和數據源方法2個表中同一類中找到然後或者設置標籤你的tableView或添加插座給他們和代理和數據源的每個功能,你可以相應地編碼作爲調用者參考可以在每種方法中找到(你可以看到它們爲(UITableView *)tableView):

if(tableView == table1 )或者如果(tableView.tag == 1) { } 其他 {}

2)創建獨立的NSObject類(比如TableClassSource),並與的UITableViewDelegate和UITableViewDataSource設置,在這裏做所有編碼。並在你的ViewController中創建這個類的對象並將你的委託和表的數據源設置爲這個對象

TableClassSource * obj = [[TableClassSource alloc] init]; table1.dataSource = obj; table1.delegate = obj;

可以在TableClassSource創建一個方法類似loadMethod,如果你需要每次

或瞭解更多的細節,你可以看到從

Handling more than 1 table in a single View- Part-1

下載示例項目更新表稱此爲重裝

Handling more than 1 table in a single View- Part-2

相關問題