2017-06-20 89 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