简体   繁体   中英

UITableView reloadData doesn't work when change sub object in datasoure.

There are 5 objects in datasoure for example,if the first object is like this:

Obj -> id:1,name:"A"

when I change the object's name to "B";

Obj -> id:1,name:"B"

then [tableView reloadData]

the first cell still display "A" ,I want to change it to "B" .

In the cellForRowAtIndexpath method manage the datasource method properly as it retrives the value from the datasource array and displays it,That is it

I doubt the problem is that the reusability is causing the trouble in your code

- (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;
    }

here is the code, what i did was,

  1. created a class called "DataClass" which provides data for your tableview
  2. created objects like you mentioned , i stored it in an array("dataSource")
  3. after that i loaded it to tableview (i assume u are properly wired up tableview datasource and delegate)
  4. I put a button to make change the string in the datasource array.
  5. button's action is connected to method and within that i am reloading the tableview


//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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM