2013-02-20 64 views
2

我有2個類,classA和classB 在classA中,我有一個tableview,可以按需使用和刷新。所有的代表和數據源都很好,還有一個屬性@property (nonatomic, retain) UITableView *myTableView;不能使用其他類的reloadData

我不想把reloadData放在viewDidAppear或viewWillAppear classA中。 我想從classB發射[myTable reloadData]

謝謝

+1

一種選擇是使用委託方法。這是一個[示例] [1]的如何。 [1]:http://stackoverflow.com/questions/8055052/call-a-parent-view-controller-through-a-navigationcontroller – user523234 2013-02-20 12:06:19

回答

16

使用委託來重新加載你的tableview。

在其中的tableview是類,在viewDidLoad中這樣寫:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateLeftTable" 
               object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(checkRes:) name:@"updateLeftTable" object:nil]; 

然後在同一個類中實現它的功能就像這樣:

-(void)checkRes:(NSNotification *)notification 
{ 
    if ([[notification name] isEqualToString:@"updateLeftTable"]) 
    { 
     [_rearTableView reloadData]; 
    } 
} 

最後,在類從那裏你想要重新加載你的桌面視圖 粘貼下面的行(重要的是這條線從你想要重新加載你的tableview的方法進入):

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateLeftTable" object:self]; 

如果您有任何問題,請告訴我。

+1

的解釋將是巨大的 – Segev 2013-02-20 12:03:28

+0

好吧,讓我解釋一下, – aBilal17 2013-02-20 12:17:10

+0

+1好的答案,並非常感謝.. – Shivaay 2014-03-11 06:21:11

0

我想你應該由代表重新加載它。

這裏您ClassB.h這裏u需要添加您的代理

@property (nonatomic, strong) id myDelegate; 

這裏您ClassA.m U時要去ClassB的表現出他的代表牛逼ClassA的

ClassB *goNext = [[ClassB alloc] initWithNibName:@"ClassB" bundle:nil]; 
goNext.myDelegate = self; // ALL YOUR BASE ARE BELONG TO US! 
[self.navigationController pushViewController:goNext animated:YES]; 

下一步。當u要重新加載您的CLASSA的tableView使用這樣的代碼:

if (myDelegate && [myDelegate respondsToSelector:@selector(reloadMyTV)]){ 
     [myDelegate performSelector:@selector(reloadMyTV) withObject:nil]; 
    } 

其中「reloadMyTV」是ClassA的方法:

-(void) reloadMyTV { 
[myTableView reloadData]; 
} 

希望我能記得的代表不夠好)我的記憶寫的) ) 希望這將有助於

+0

的解釋將是巨大的 – Segev 2013-02-20 12:04:00

0

documentation

爲了提高效率,表格視圖僅重新顯示那些可見的行。

因此,如果您的tableView不可見,那麼它將不會被更新。

1

當你調用classA * test = [[classA alloc] init]; ,它會創建A類的新對象,並且對於您在此類中聲明的每個變量都沒有值。這就是爲什麼你的reloaddata沒有被調用becoz你的數組沒有這個對象的值,你正用它來重新加載表。

您必須使用委託將對象從類A傳遞給類B.然後嘗試從此委託對象重新加載表。

1
ClassB.h 

    @protocol ClassBDelegate <NSObject> 

    -(void)reloadTableView; 


    @end 

    @property (nonatomic,weak)id<ClassBDelegate>delegate; 


    ClassB.m 

    -(void)methodForReloadingTableViewInClassA 
    { 

    [self.delegate reloadTableView]; 

    } 




Class A.h 
#import ClassB.h 
@interface ClassA : UITableViewController<UITableViewDelegate,UITableViewDatasource,ClassBDelegate> 
{ 
} 

ClassA.m 
-(void)createClassB 
{ 
    ClassB *obj = [[ClassB alloc]init]; 
    obj.delegate = self; 
} 

    //delagte method of class B 
    -(void)reloadTableView 
    { 

    [self.tableView reloadData]; 

    } 
+0

我得到'找不到'ClassBDelegate'的協議聲明' – Segev 2013-02-20 12:29:52

+0

您是否在ClassA.h中導入了ClassB.h? – 2013-02-20 12:47:34

+0

是的,我做到了。甚至在一個新項目中嘗試過。 – Segev 2013-02-20 12:48:50

0

調用reloadData方法一旦調用方法就會刷新數據。在調用reloadData之前,請確保數據源(數組或字典或保存值的任何位置)已更改。

你應該嘗試重新加載主線程上的數據: [tableView performSelectorOnMainThread:@selector(reloadData)withObject:nil waitUntilDone:NO];

0

支票是你的UITableView屬性是在您撥打reloadData時有效。 UIViewController通過調用alloc init創建,但您的視圖當前處於無效狀態。在reloadData代碼行中設置斷點並檢查是否.tableView == nil?