清除 reloadData 上的 tableView 不起作用?
我正在为我们公司编写一个简单的内部经济学应用程序,但我遇到了一些问题。我用来自动态生成的对象的信息填充 UITableView,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Payments *project = [appDelegate.projects objectAtIndex:indexPath.row];
if([project.parentProject isEqualToString:bottomTabBar.selectedItem.title]) {
NSLog(@"%@ är lika med %@ index: %d",project.parentProject, bottomTabBar.selectedItem.title, indexPath.row);
// Configure the cell...
NSMutableString *changeInValue = [[NSMutableString alloc] initWithFormat:@"%d",[[project.amountsek objectAtIndex:0] intValue]-[[project.amountsek objectAtIndex:1] intValue]];
if([changeInValue intValue] >= 0) {
[changeInValue insertString:@"+" atIndex:0];
cell.imageView.image = [UIImage imageNamed:@"up.png"];
} else {
cell.imageView.image = [UIImage imageNamed:@"down.png"];
}
NSMutableString *foreignCurrency = [[NSMutableString alloc] initWithString:@""];
if(![project.currency isEqualToString:@"SEK"]) {
[foreignCurrency appendFormat:@" - %@%d",project.currency,[[project.payments objectAtIndex:0] intValue]];
}
NSString *detailString = [[NSString alloc] initWithFormat:@"%@%d (%@)%@",@"SEK",[[project.amountsek objectAtIndex:0] intValue],changeInValue, foreignCurrency];
[changeInValue release];
[foreignCurrency release];
cell.textLabel.text = project.name;
cell.detailTextLabel.text = detailString;
[detailString release];
}
project = nil;
return cell;}
一切都像魅力一样!然而!当我按下另一个 tabButton 时,我希望它重新加载表格并仅显示匹配的元素! (匹配工作正常,日志正确打印出所有内容)尽管如此,在添加新单元格之前旧表格单元格不会清空。
这是重新加载 tabItem 的代码:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"Tab clicked: %d", item.tag);
[sourcesTable reloadData];
}
我该如何解决这个问题? 我是 iPhone 编程新手,我确实需要一些帮助。
I'm programming a simple in-house economics app for our company, but I'm facing some problems. I populate a UITableView with information from dynamically generated objects like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Payments *project = [appDelegate.projects objectAtIndex:indexPath.row];
if([project.parentProject isEqualToString:bottomTabBar.selectedItem.title]) {
NSLog(@"%@ är lika med %@ index: %d",project.parentProject, bottomTabBar.selectedItem.title, indexPath.row);
// Configure the cell...
NSMutableString *changeInValue = [[NSMutableString alloc] initWithFormat:@"%d",[[project.amountsek objectAtIndex:0] intValue]-[[project.amountsek objectAtIndex:1] intValue]];
if([changeInValue intValue] >= 0) {
[changeInValue insertString:@"+" atIndex:0];
cell.imageView.image = [UIImage imageNamed:@"up.png"];
} else {
cell.imageView.image = [UIImage imageNamed:@"down.png"];
}
NSMutableString *foreignCurrency = [[NSMutableString alloc] initWithString:@""];
if(![project.currency isEqualToString:@"SEK"]) {
[foreignCurrency appendFormat:@" - %@%d",project.currency,[[project.payments objectAtIndex:0] intValue]];
}
NSString *detailString = [[NSString alloc] initWithFormat:@"%@%d (%@)%@",@"SEK",[[project.amountsek objectAtIndex:0] intValue],changeInValue, foreignCurrency];
[changeInValue release];
[foreignCurrency release];
cell.textLabel.text = project.name;
cell.detailTextLabel.text = detailString;
[detailString release];
}
project = nil;
return cell;}
And everything works like a charm! However! When I press another tabButton I want it to reload the table and to display only the matched elements! (The matching works fine, the log prints out everything correctly) Although, the old table cells does not empty before the new ones are added.
Here's the code for the reload tabItem:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"Tab clicked: %d", item.tag);
[sourcesTable reloadData];
}
How do I solve this?
I'm new to programming for the iPhone and I could really use some help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请调用[sourcesTable reloadData];在 ViewController.m 的 viewWillAppear 方法中
每当您的视图出现时都会出现此调用。
Please call [sourcesTable reloadData]; in viewWillAppear method of that ViewController.m
This call every time when your view is appears.