2010-04-17 103 views
2

我有一個奇怪的問題。我創建了一個名爲SampleTable的正常Objective c類。然後我擴展了UITableView而不是NSObject。然後在initWithFrame構造函數中初始化表。我也爲數據源創建了一個NSMutableArray對象。我也符合UITableViewDelegate和UITableViewDatasource。我也覆蓋了必要的方法。UITableView沒有得到填充

現在我在另一個類中創建了這個類的對象,並將該對象添加爲子視圖。 tableView根據我給予initWithFrame構造函數的CGRectMake()座標繪製。但它沒有得到充分的數據。我不知道問題是什麼。可以找人幫我。

SampleTable.h

#import <Foundation/Foundation.h> 

@interface SampleTable : UITableView { 

    NSMutableArray *ItemArray; 

} 

@property (nonatomic,retain) NSMutableArray *ItemArray; 

-(NSMutableArray *) displayItemArray; 

@end 

SampleTable.m

#import "SampleTable.h" 

@implementation SampleTable 

@synthesize ItemArray; 

-(id)initWithFrame:(CGRect)frm { 

    [super initWithFrame:frm]; 

    self.delegate=self; 

    self.dataSource=self; 

    [self reloadData]; 

    return self; 

} 

-(NSMutableArray *) displayItemArray { 

    if(ItemArray==nil) { 

     ItemArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil]; 

    } 

    return ItemArray; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return 1; 

} 

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [ItemArray count]; 

} 

-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell==nil) { 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

    [cell autorelease]; 

    } 

    NSString *temp=[self.ItemArray objectAtIndex:indexPath.row]; 

    cell.textLabel.text = temp; 

    return cell; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSLog(@"didselect"); 

} 

-(void) dealloc { 

    [ItemArray release]; 

    [super dealloc]; 

} 

    @end 

+0

我會再來。從長遠來看,將代碼放入一個'UITableViewController'子類中是一個好主意,它也負責創建'UITableView'實例。業務代碼不是可視對象。 – 2010-04-17 07:16:43

+0

其實我試圖做一個下拉框。所以當我點擊一個按鈕時,SamplTable的一個對象被創建並被添加到按鈕的子視圖中。我已經創建了按鈕以及UITableView動態。所以我不想使用UITableViewController。 – Jayshree 2010-04-17 07:20:39

+0

對,周杰倫 - 我沒有刪除我的評論推薦UITableViewController,因爲我懷疑某事像一個嵌入式UITableView更大的東西。當談到最常見的用法時,Ralf當然是對的。儘管如此,我仍然強烈建議不要創建這種自定義控件,因爲用戶可能不習慣使用它們。 – Till 2010-04-17 07:30:55

回答

3

你也從來沒有您的代碼段中初始化ItemArray。取出displayItemData方法和改變初始走向:

 

-(id)initWithFrame:(CGRect)frm 
{ 
    if ((self = [super initWithFrame:frm])) != nil) 
    { 
     self.delegate=self; 
     self.dataSource=self; 
     ItemArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil]; 
     [self reloadData]; 
    } 
    return self; 
} 
 

你也可以簡單地調用初始化內displayItemArray方法。我覺得這種方法毫無意義,因此我建議徹底刪除它。

沒有自己嘗試,我仍然非常有信心,你也可以在初始化器中擺脫[self reloadData]。

0

[self.ItemArray objectAtIndex:indexPath.row]; 
中的NSLog

打印值...請檢查是否它打印有效值或不....似乎陣列的對象釋放一些e之前..

+0

還在(NSInteger)tableView中打印[self.ItemArray count]的值:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section – Till 2010-04-17 07:03:31

1

-(id)initWithFrame:(CGRect)frm方法期間,您的代碼在UITableView上調用-reloadData。此時您的ItemArray不可用。

因此,當UITableView稱它爲代表-numberOfSectionsInTableView:-tableView:numberOfRowsInSection:方法來獲取有關視圖中顯示內容的信息時,它們將返回零行的一個部分。

沒有顯示!

這可能是1(一)解決方案來改變你的ItemArray的初始化:

-(NSMutableArray *) displayItemArray { 
    if(ItemArray==nil) { 
     ItemArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil]; 
     [self reloadData]; 
    } 
    return ItemArray; 
}