2011-03-25 83 views
7

我是新的objective-c。我有一個3行1節的tableView。你能幫助我如何通過按下按鈕(addCity)添加行中的一些文本?如何在表格中插入行?

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    m_bg.image = [UIImage imageNamed:[WeatherAppDelegate isNight] ? @"SettingsBackNight.png" : @"SettingsBackDay.png" ]; 
    tableView.layer.cornerRadius = 10; 
    m_scrollView.layer.cornerRadius = 10; 
    [m_scrollView setContentSize:CGSizeMake(tableView.frame.size.width, tableView.frame.size.height)]; 
    dataArray = [[NSMutableArray alloc] initWithObjects:@"Moscow", @"London", @"Paris", nil]; 

} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    [cell.textLabel setText: [dataArray objectAtIndex:indexPath.row]]; 
    return cell; 
} 


-(IBAction)addCity:(id)sender 
{ 
    [dataArray addObject:@"City"]; 
    NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]]; 
    [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop]; 
    [tableView reloadData]; 
} 
+0

我已更新我的代碼。它不起作用。 – Sveta 2011-03-25 08:38:52

+0

我被刪除insertRowsAtIndexPaths和我的程序正在工作! – Sveta 2011-03-25 08:56:04

回答

13

您的表格必須從某些來源獲取其數據,您可以在需要時添加元素。 (比如數據源中的NSMutableArray實例),那麼你的方法看起來就像。爲簡單起見,假設您有包含NSString的dataArray:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 
    [cell.textLabel setText: [dataArray objectAtIndex:indexPath.row]; 
    return cell; 
} 


-(IBAction)addCity:(id)sender 
{ 
    [tableView beginUpdates]; 
    [dataArray addObject:@"City"]; 
    NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]]; 
    [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop]; 
    [tableView endUpdates]; 
} 
+0

這樣的事情? dataArray = [[NSMutableArray alloc] initWithObjects:@「Moscow」,@「London」,@「Paris」,nil];我的程序崩潰了。 – Sveta 2011-03-25 08:26:20

+0

@Sveta,是的數組應該以這種方式創建...你還有崩潰嗎? – Vladimir 2011-03-25 08:57:28

+0

不,我被刪除insertRowsAtIndexPaths和我的程序正在工作! – Sveta 2011-03-25 09:00:46

2

您不直接將行插入到tableView中。你採用了UITableViewDataSource協議。您的數據源爲表視圖對象提供了構建和修改表視圖所需的信息。 UITableViewDataSource Protocol Reference

另外,看着你的示例代碼,你硬編碼了表中的行數爲3.因此,UITableView只會顯示3行。你需要像這樣的東西:

// You have a city array you created 
NSMutableArray *cityArray = ...... 

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

-(IBAction)addCity:(id)sender 
{ 
    // create a new city object 
    // add the object to your array 
    [cityArray addObject:.... 
    // refresh the table 
    [tableView reloadData]; 
} 
+0

我認爲他想插入單獨的行,並有一個很好的動畫,您的方法不提供一個很好的動畫。 – 2016-01-13 05:53:47