在tableview中添加对象

时间:2017-06-20 16:16:46

标签: ios objective-c uitableview

我想在tableview中添加一个对象来动态插入行。

代码似乎没有工作,没有插入行。

代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *newString = @"test!";

    [self.greekLetters addObject:newString];
}

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

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSString *SimpleIdentifier = @"SimpleIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleIdentifier];

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

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [self.greekLetters objectAtIndex:indexPath.row]];

    cell.detailTextLabel.text = @"Laatste datum";

    return cell;
}

该对象未添加到textlabel文本,并且没有新行。我是这类iOS的新手,所以代码有什么问题?

我尝试了很多东西,却无法让它发挥作用。

感谢。

1 个答案:

答案 0 :(得分:0)

您好,请找到以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.greekLetters = [[NSMutableArray alloc] init];

    NSString *newString = @"test!";

    [self.greekLetters addObject:newString];
}

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

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

    NSString *SimpleIdentifier = @"SimpleIdentifier";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleIdentifier];

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

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [self.greekLetters objectAtIndex:indexPath.row]];

    cell.detailTextLabel.text = @"Laatste datum";


    return cell;
}