2014-03-03 52 views
1

我遇到了問題。我有一個我用編程方式創建的TableView,它不顯示我已經添加的對應於TableView行的TextView。我無法在堆棧 溢出中找到此解決方案。Xcode tableview不顯示文字

#import "MasterTableViewController.h" 
@interface MasterTableViewController() 
@end 
@implementation MasterTableViewController 

-(NSMutableArray *)numbers { 
    if(!_numbers) 
    { 
     _numbers = [[NSMutableArray alloc] init]; 
    } 
    return _numbers; 
} 

-(NSMutableArray *)subtile { 
    if(!_subtile) 
    { 
     _subtile = [[NSMutableArray alloc] init]; 
    } 
    return _subtile; 
} 

- (void)viewDidLoad 
{ 
    //Adding Titles 
    [self.champions addObject:@"1"]; 
    [self.champions addObject:@"2"]; 

    //Adding subtitles 

    [self.subtileChamps addObject:@"One"]; 
    [self.subtileChamps addObject:@"Two"]; 
    [super viewDidLoad]; 
} 

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return self.numbers.count; 
} 

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

    // Configure the cell... 

    cell.textLabel.text = self.numbers[indexPath.row]; 
    cell.detailTextLabel.text = self.subtile[indexPath.row]; 

    return cell; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
@end 
+0

它在哪裏顯示不正確;在實際應用中?在TableView的行內?我很確定你在問我的朋友是什麼:)。 – Tukajo

+0

這是一個自定義單元格還是您正在嘗試使用默認單元格? –

+0

在你的代碼中你沒有在數組中添加任何對象,你可以確保numberOfRowsInSection:方法被調用,以及數組的數量是多少。 – prasad

回答

2
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle: reuseIdentifier:] 
} 

你應該初始化你的

而且......你有你的啓動變量的常用方法。這個應該適合你。該方法在所有其他方法之前調用,並且您可以在此啓動變量:

-(id)initWithStyle:(UITableViewStyle)style 
{ 
    if (self = [super initWithStyle:style]) 
    { 
     someArray = [NSMutableArray array]; //init the array 
    } 

    return self; 
}