【问题标题】:-tableView:cellForRowAtIndexPath: is not getting called-tableView:cellForRowAtIndexPath: 没有被调用
【发布时间】:2012-10-19 10:34:33
【问题描述】:

我在UIViewController 中有UITableView,并且我已经通过插座连接,并且已将数据源分配给self。numberOfSectionsInTableViewnumberOfRowsInSection 被调用我尝试使用NSLog,但cellForRowAtIndexPath没有接到电话我的代码看起来像

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

    NSLog(@" cellForRowAtIndexPath ");

    static NSString *CellIdentifier = @"CurrencyCell";

    CurrencyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CurrencyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
    }
    new *Obj = [appdelegate.array objectAtIndex:indexPath.row];


    cell.CurNameLabel.text = Obj.C_Name;
    cell.textLabel.text = @"sdfnhsdfndsio;";
    NSLog(@"C_Name %@", Obj.C_Name);

    return cell;
}

谁能告诉我我犯错了提前谢谢

【问题讨论】:

  • 你是否也在 IB 中连接了代理和数据源
  • 您在numberOfSectionsInTableViewnumberOfRowsInSection 中返回什么值?
  • CurrencyCell 是您的自定义单元格??
  • CurrencyCell *cell = (CurrencyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  • "yourArray" 可能为空,这就是为什么不调用 cellForRowAtIndexPath 方法

标签: iphone uitableview ios5 xcode4


【解决方案1】:

似乎返回的numberOfSectionsInTableViewnumberOfRowsInSection 为0。如果是这种情况,则不会调用cellForRowAtIndexPath..

从上面的评论..似乎“YourAppDelegate”变量是null或yourArray是null。尝试在这些委托上保持断点,检查它们返回的值

【讨论】:

  • 行数为3,节数为1怎么办?
【解决方案2】:

试试这个 在 .h 文件中添加委托和数据源,如下所示..

@interface AddProjectViewController : UIViewController<
UITableViewDataSource,UITableViewDelegate>
.....
@end

viewDidLoad: 之后输入此代码..

    - (void)viewDidLoad
   {
        yourTableView.delegate= self;
        yourTableView.dataSource=self;
   }

最后尝试在numberOfRowsInSection 方法中返回一些大于0 的int

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [yourArray count];//or any int value
}

【讨论】:

  • 我这样做了 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [appdelegate.array count]; }
  • 你检查你的数组不是 nil 吗??
【解决方案3】:

如果您在应用中启用了多线程概念,即使您在 tableview 上调用 reloadData 方法,有时也不会调用 cellForRowAtIndexPath。所以像这样调用 realod 方法:

[self performSelectorOnMainThread:@selector(reloadTable) withObject:self waitUntilDone:NO];

它可能会起作用。

- (void)reloadTable
{
    [self.tableView reloadData];
}

如果您不在多线程环境中,则需要确保tableviews delegatedatasource 不为空。

【讨论】:

    【解决方案4】:

    似乎返回的numberOfSectionsInTableViewnumberOfRowsInSection 为0,或者可能是另一个原因

    仔细检查以下几点

    1如果您从 XIB 创建 TableView,则应设置数据源并委托给文件的所有者。

    如果您以编程方式创建它,那么您应该如下设置委托和数据源,不要忘记在.h 中采用dataSourecdelegateUItableView

       YourViewController : UIViewController<UITableViewdelegate, UITableViewDatasource>
    

    将下面的代码行放在创建 UITableView 的行之后

            tableViewObj.delegate= self;
            tableViewObj.dataSource=self
    

    2仔细检查您的datasource(无论数组的名称是什么)是否有数据,因为每当您创建行数时,您都会传递数组`count as given below

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
         {  
          int  numberOfRows= [datasource count];
          return numberOfRows;
          //  here check does the `numberOfRows` have nonzero value or just 0
          if it retunes 0 then cellForAtIndexPath would not call .
       }
    

    希望对你有帮助

    【讨论】:

      【解决方案5】:

      在“numberOfRowsInSection”方法中检查数组中的数值。打印你的数组 在返回值之前。

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          NSLog(@"%@",returnedArray);
          return [returnedArray count];//
      }
      

      您也可以尝试在您的方法中发送一些静态行数来检查。

      看起来您的数组在该方法中为空。

      希望它能解决您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 2012-02-08
        • 2011-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多