Xcode tableview不显示文本

时间:2014-03-03 20:34:53

标签: ios objective-c uitableview uitextview

我遇到了一个问题。我有一个以编程方式创建的TableView,它不显示我添加的对应于TableView行的TextView。我无法在Stack Overflow上找到解决方案。

#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

1 个答案:

答案 0 :(得分: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;
}