有在datasoure例如5名對象,如果第一對象是這樣的:當更改datasoure中的子對象時,UITableView reloadData不起作用。
Obj -> id:1,name:"A"
當我改變該對象的名稱爲「B」
;
Obj -> id:1,name:"B"
然後[tableView reloadData]
第一小區仍顯示「A」,我想將其改變爲「B」。
有在datasoure例如5名對象,如果第一對象是這樣的:當更改datasoure中的子對象時,UITableView reloadData不起作用。
Obj -> id:1,name:"A"
當我改變該對象的名稱爲「B」
;
Obj -> id:1,name:"B"
然後[tableView reloadData]
第一小區仍顯示「A」,我想將其改變爲「B」。
cellForRowAtIndexPath方法中正確地管理數據源方法,因爲它retrives從數據源陣列的價值,並將其顯示,這就是它
我懷疑的問題是,可重用性導致在你的代碼的麻煩
- (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;
}
這裏是代碼,我所做的就是,
//類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->[email protected]"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"
這就是我所做的,但第3行不會顯示「世界」,因爲**重用**單元格。 @ Lithu T.V的答案是正確的,只是讓該單元格不被重用。 – Mil0R3
多數民衆贊成在我做儀式,初始化只有單元格爲零時,這將發生在該開始之後,UITableview將重新使用這些單元這就是爲什麼你應該初始化它在旁邊的if塊 –
你能提供一些示例代碼? – Raspu
如果您可以提供一些示例代碼,您會如此友善。 Thanx !!!。 – madLokesh
1st確保您的tableView獲取**重新加載**並使用對象檢查數據源。 –