2017-06-20 54 views
0

我想在我的tableview中添加一个对象来动态插入行。在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很陌生,所以代码有什么问题?

我尝试了很多东西,但无法让它工作。

谢谢。

+0

'self.greekLetters'它在哪里初始化?它是'零'吗? – Larme

+0

在viewcontroller.h中。 @属性(复制,非原子)NSMutableArray * greekLetters; – Anoniem

+1

你在哪里做'self.greekLetters = [[NSMutableArray alloc] init];'。 – Larme

回答

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; 
} 
+1

请不要只是发布代码。解释问题所在,并解释您的答案如何解决问题。 – rmaddy