无法从另一个视图中填充NSMutable数组中的UITableView

时间:2012-07-27 08:26:19

标签: iphone objective-c ios xcode uitableview

我有2个VC,首先是BooksDetailViewController,它有NSString myBookString。另一个是FavBooksViewController,它有favBookList NSMutableArray。我想从第一个VC传递NSString myBookString以将其包含在favBookList NSMutableArray中,然后填充该表。

我在@class FavBooksViewController;

中添加了BooksDetailViewController.h

#import "FavBooksViewController.h"

中导入的BooksDetailViewController.m

BooksDetailViewController.m中的操作如下所示:

- (IBAction)addToFav:(id)sender {
    FavBooksViewController *mdvc;
    [mdvc.self.favBookList addObject:myBookString];
}

FavBooksViewController.h我有

@interface FavBooksViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) id delegate;
@property (nonatomic,strong) NSMutableArray *favBookList;

FavBooksViewController.m - #import "FavBooksDetailViewController.h" 在ViewDidLoad中,我试图包含这一行

self.favBookList = [[NSMutableArray alloc]initWithObjects:nil];

但我已经评论过了。有了它我们没有它UITableView无法正常工作。 当然,我有这个:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.favBookList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
           UITableViewCell *cell = [tableView 
                                 dequeueReusableCellWithIdentifier:@"favBooksCell"];

        cell.textLabel.text = [self.favBookList
                               objectAtIndex:indexPath.row];
        return cell;
}

无论如何,UITableView只是空的,没有单元格,没有。 这可能是什么错误?我究竟做错了什么? 提前谢谢!

2 个答案:

答案 0 :(得分:2)

您忘记使用cellForRowAtIndexPath方法启动单元格。

代码应为:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
        static NSString *identifier = @"favBooksCell";

        UITableViewCell *cell = [tableView 
                                 dequeueReusableCellWithIdentifier:identifier];
        if(!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }

        cell.textLabel.text = [self.favBookList
                               objectAtIndex:indexPath.row];
        return cell;
}

试试这个; - )

希望这有帮助。

答案 1 :(得分:2)

不是将字符串添加到 BooksDetailViewController 中的数组,只需将字符串传递给 FavBooksViewController ,然后将其添加到FavBooksViewController中的数组中。

- (IBAction)addToFav:(id)sender {
    FavBooksViewController *mdvc;
    [mdvc setFavBook:myBookString];  
    //favBook is a NSString member object in your FavBooksViewController class

     [self.navigationController pushViewController:mdvc animated:YES];
}

试试这是否有效。