我是新的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];
}
我已更新我的代碼。它不起作用。 – Sveta 2011-03-25 08:38:52
我被刪除insertRowsAtIndexPaths和我的程序正在工作! – Sveta 2011-03-25 08:56:04