2013-07-19 33 views
1

好吧,它目前的狀態如何;我有一個名爲PlaylistController的UIViewController(用於整潔的自定義類)。該控制器實現的UITableViewDelegate和UITableViewDataSource協議和相當粗暴地從一個NSMutableArray的一些基本信息填充的UITableView:Obj-C:UITableView更新數據源交叉類

PlaylistController.h:

@interface PlaylistController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 
    @public NSMutableArray* _playlists; 
    @public NSMutableArray* _tracks; 
} 

@property (nonatomic, strong) IBOutlet UITableView *tableView; 

PlaylistController.m:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    tableView.delegate = self; 
    tableView.dataSource = self; 
    _playlists = [[NSMutableArray alloc] initWithObjects:@"Heyy", @"You ok?", nil]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { 
    return [_playlists count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"CellIdentifier"; 

    // Dequeue or create a cell of the appropriate type. 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [_playlists objectAtIndex:indexPath.row]]; 
    return cell; 
} 

其中一期工程很好,當我點擊相應的標籤來顯示UIViewController時,它全部被填充。當新數據變得可用時,我的問題就伴隨着更改數據源。

考慮到新數據來自不同的類,我將如何去更新數據源?辛格爾頓?

+0

問題是什麼?你在'_playlists'和'_tracks'中設置了新的值,並調用'[tableView reloadData];'讓一切都自動處理,不是? – dasblinkenlight

+0

對不起,我可能不清楚!它更**我怎麼**更新'_playlists' **從另一個類**,然後調用'[tableView reloadData];' – Jordan

+0

你有什麼「其他類」?如果屏幕上出現「PlaylistController」,則沒有其他可以控制的類;如果它不在屏幕上,'viewWillAppear'可以處理更新。 – dasblinkenlight

回答

0

將您的「播放列表」數組作爲視圖控制器上的公共屬性公開。實現一個自定義setter,在設置時提示tableview重載數據:

@property (strong, nonatomic) NSArray* playlists; 

... 

@synthesize playlists=_playlists; 

... 

- (void) setPlaylists: (NSArray*) playlists 
{ 
    _playlists = playlists; 

    if (self.isViewLoaded) 
    { 
     [self.tableView reloadData]; 
    } 
}