簡體   English   中英

在datasoure中更改子對象時,UITableView reloadData不起作用。

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

例如,datasoure中有5個對象,如果第一個對象是這樣的:

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

當我將對象的名稱更改為“ B”時;

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

然后[tableView reloadData]

第一個單元格仍顯示“ A” ,我想將其更改為“ B”

在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. 創建了一個名為“ DataClass”的類,該類為您的表視圖提供數據
  2. 創建了您提到的對象,我將其存儲在array(“ dataSource”)中
  3. 之后,我將其加載到tableview(我假設您已正確連接tableview數據源和委托)
  4. 我放置了一個按鈕來更改數據源數組中的字符串。
  5. 按鈕的動作連接到方法,並且在其中我正在重新加載tableview


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM