【问题标题】:Xcode tableview does not show the textXcode tableview 不显示文本
【发布时间】:2014-03-03 20:34:52
【问题描述】:

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

#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

【问题讨论】:

  • 哪里没有准确显示;在实际应用中?在 TableView 的行内?我很确定你在问我的朋友什么:)。
  • 这是自定义单元格还是您尝试使用默认单元格?
  • 在您的代码中,您没有将任何对象添加到 numbers 数组中,您能否确保 numberOfRowsInSection: 方法被调用,以及数组的计数是多少。
  • 您似乎没有正确处理此问题。从工作表视图的一些示例代码开始。运行示例代码,然后对其进行更改以执行您想要的操作。否则,你只是在黑暗中拍摄。
  • 要尝试的一些事情: (1) 使用调试器单步执行? (2) 添加一些日志记录以验证您对值应该是什么的期望 (3) 创建一个更简单的版本,然后对其进行单元测试,然后再处理更复杂的问题?

标签: ios objective-c uitableview uitextview


【解决方案1】:
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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    相关资源
    最近更新 更多