【问题标题】:Print NSMutableArray into UITableView将 NSMutableArray 打印到 UITableView
【发布时间】:2017-07-16 16:27:04
【问题描述】:

在我的应用程序中,我有一个带有 barbuttonitem 的 tableview。当我按下 barbuttonitem 时,我看到一个允许我输入数据的警报控制器。然后稍后,将这些数据保存在 NSMutableArray 中,我的目的是在表格视图的单元格中打印这些数据之一。问题是数据获取是正确的,但是tableview仍然是空的。不明白为什么,好像没事!

类:FeedInfo

#import <Foundation/Foundation.h>

@interface FeedInfo : NSObject
@property (nonatomic,strong) NSString *feedURL;
@property (nonatomic,strong) NSString *feedTitle;
@property (nonatomic,strong) NSString *feedCategory;
@end

然后我在 tableview 中有一个 barbuttonitem。当我点击这个按钮时,我会打开一个控制器警报,允许我输入 3 个字段:url、feed、category。

我已经声明@property(nonatomic,strong) NSMutableArray *feedArray;

然后我将这些日期插入 NSMutableArray。

- (IBAction)inserFeed:(UIBarButtonItem *)sender {
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Feed RSS: info?"
                                                                              message: @""
                                                                       preferredStyle:UIAlertControllerStyleAlert];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Feed URL";
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Feed Title";
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Feed category";
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;

    }];

    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSArray * textfields = alertController.textFields;

        UITextField * urlfield = textfields[0];
        UITextField * titlefield = textfields[1];
        UITextField * categoryfield = textfields[2];

        FeedInfo *newFeed = [[FeedInfo alloc] init];
        newFeed.feedURL = urlfield.text;
        newFeed.feedTitle = titlefield.text;
        newFeed.feedCategory = categoryfield.text;


        [self.feedArray addObject:newFeed];
        [self.tableView reloadData];
    }]];

    [self presentViewController:alertController animated:YES completion:nil];
}

这里一切正常。问题是在我的表格视图的单元格中打印这些字段之一。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    FeedInfo *feedToShow = [self.feedArray objectAtIndex:indexPath.row];
    cell.textLabel.text = feedToShow.feedTitle;
    return cell;
}

【问题讨论】:

  • 您是否将TableViewDatasource 协议分配给您的控制器?您是否覆盖了numberOfRowsInSection
  • @dirtydanee 我在 viewDidLoad 中输入了self.tableView.delegate = self; self.tableView.dataSource = self;。至于方法,我写了-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
  • -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.feedArray.count; }

标签: ios objective-c uitableview


【解决方案1】:

您的单元格创建有问题。你的cell 对象是nil

使用以下代码获取单元格对象。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

dequeueReusableCellWithIdentifier: forIndexPath : 较新的 dequeue 方法保证单元格被返回并正确调整大小,假设标识符已注册

如果您没有在xib/storyboard 上设置cell identifier,那么 您必须调用registerClassregisterNib 才能使用dequeueReusableCellWithIdentifier:forIndexPath: 方法。

更新:

数据源数组未分配。在viewDidLoad()内分配feedArray

- (void)viewDidLoad {
   [super viewDidLoad]; 
   self.feedArray = [[NSMutableArray alloc]init];
}

【讨论】:

  • 结果相等,tableview 为空 :(
  • 您是否在 xib/storyboard 上添加了单元格标识符?
  • 是的,我的单元格标识符是“单元格”
  • @Taprolano 你的细胞等级是什么?你能放断点并检查单元格对象是nil还是不是
  • 我在单元格创建后立即插入了此检查if (cell == nil) { NSLog(@"error"); } 并且没有打印任何内容。所以我想细胞不是零
猜你喜欢
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 2014-07-15
  • 1970-01-01
相关资源
最近更新 更多