【问题标题】:UITableview prints only first object of NSMutableArray , IOSUITableview 仅打印 NSMutableArray 的第一个对象,IOS
【发布时间】:2012-11-09 15:20:28
【问题描述】:

我有一个 NSMutableArray 和一个从 NSMutableArray 填充的分组表视图。

当我用 NSLog 打印 NSmutableArray 时,输出是 "String1","String2","String3"

但在 UITableView Cell 上我总是看到 NSMUtableArray 的第一项:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return [_presenterList count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSLog(@"_presenterList objectAtIndex: %@",[_presenterList objectAtIndex:indexPath.row]);

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

输出 NSlog "String1"

tableview 上的输出

String1
String1
String1

我做错了什么?

添加了编辑屏幕截图

我从字符串和 1 个 tableview 行创建了一些标题,如图中所示

【问题讨论】:

    标签: objective-c ios uitableview nsmutablearray


    【解决方案1】:
    cell.textLabel.text=[_presenterList objectAtIndex:indexPath.section];
    

    而不是 cell.textLabel.text=[_presenterList objectAtIndex:indexPath.row];

    【讨论】:

      【解决方案2】:

      您在numberOfRowsInSection[array count]numberOfSectionsInTableView 中返回“1”。这是倒退的(除非您真的想要数组中的每个项目都有一个部分。

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          return [_presenterList count];
      }
      
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
          return 1;
      }
      

      【讨论】:

      • 如果您打算将此更新发布到tableView:numberOfRowsInSection:,您还应该将所需的更新发布到numberOfSectionsInTableView:
      • @NSPostWhenIdle 我只需要一个等于 NSMutableArray Count 的行和节数,请参阅已编辑问题的屏幕截图,所以你的回答给了我 1 个节和行等于 NSmutablecount,这与我需要什么
      • @MordFustang 如果这是您真正想要采用的设计,请查看下面的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多