例如,如果第一个对象是这样的,那么数据中有5个对象:
Obj -> id:1,name:"A"
当我将对象的名称更改为“B”时;
Obj -> id:1,name:"B"
然后[tableView reloadData]
第一个单元格仍显示“A”,我想将其更改为“B”。
答案 0 :(得分:1)
在cellForRowAtIndexpath方法中正确管理数据源方法,因为它从数据源数组中检索值并显示它,就是它
我怀疑问题是可重用性导致代码出现问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *cellIdentifier1 = @"surveyCell";
if (tableView== self.surveytableView) {
cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
if(cell==nil)
{
//alloc the cell
//DO NOT SET THE VALUE HERE
}
//here set the value
return cell;
}
答案 1 :(得分:0)
这是代码,我做的是,
// class DataClass
@interface DataClass : NSObject
{
@public;
NSString *str;
}
@implementation DataClass
- (id)init
{
[super init];
str = nil;
return self;
}
@end
//viewController.h
@interface ViewController : UIViewController<UITableViewDataSource ,UITableViewDelegate>
{
IBOutlet UITableView *aTableView;
IBOutlet UIButton *aButton;
}
- (IBAction)whenButtonClicked:(id)sender;
@end
//in .m file
#import "ViewController.h"
#import "DataClass.h"
@interface ViewController ()
{
NSMutableArray *DataSource;
}
@end
// ViewController.m
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aTableView.dataSource = self;
aTableView.delegate = self;
DataSource = [[NSMutableArray alloc]init];
for(int i=0 ; i<5 ; i++)
{
DataClass *data = [[DataClass alloc]init];
data->str=@"hello";
[DataSource addObject:data];
[data release];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(aCell == nil)
{
aCell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]autorelease];
}
DataClass *aValue = [DataSource objectAtIndex:indexPath.row];
aCell.textLabel.text = aValue->str;
return aCell;
}
- (IBAction)whenButtonClicked:(id)sender
{
DataClass *aObj = [DataSource objectAtIndex:2];//changing the 3'rd object value as yours in 5th object
aObj->str = @"world";
[aTableView reloadData];
}
@end
//after "changeValue" button pressed the third row displays "world"