2014-10-20 63 views
0

我试图做一个基本的自定义单元格,并在我跟着教程后,我得到了这个问题。UITableView数据源问题

reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

- (NSInteger)numberOfSections; 
{ 
    return 1; 
} 


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

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

CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

/*if (! customCell) { 
    NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"cell" owner:nil options:nil]; 
    customCell = [parts objectAtIndex:0]; 
} 
*/ 

customCell.lblTime.text = [array1 objectAtIndex:indexPath.row]; 
customCell.lblEvent.text = [array2 objectAtIndex:indexPath.row]; 

return customCell; 
} 

而且我已经设置在的tableView身份但仍不能正常工作,我用故事板。此外,我已经链接了datasourcedelegate。请问哪里是我的问题?

+0

您是否尝试过打破了'的cellForRowAtIndexPath:'方法,看是否customCell是零之后调用中退出? – Jonathan 2014-10-20 14:13:52

+0

请检查您的单元格是静态的还是动态的,并检查您的.m委托类中的段和行是否给出了正确的编号 – 2014-10-20 14:14:56

+0

@Jonathan我现在检查了customCell是否为零。我该怎么办? – CAN 2014-10-20 14:15:57

回答

0

在第一次执行时,单元格为零。所以编写代码来初始化单元格。

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

    static NSString *CellIdentifier = @"MainCell"; 

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) { 
     NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; 
     cell = [parts objectAtIndex:0]; 
    } 

    cell.lblTime.text = [array1 objectAtIndex:indexPath.row]; 
    cell.lblEvent.text = [array2 objectAtIndex:indexPath.row]; 

    return cell; 
} 
+0

它正在运行,但表格视图是空的。你能帮助我吗? – CAN 2014-10-21 08:58:50

+0

确保变量lblTime,lblEvent正确映射到存在于CustomCell中的标签。 – 2014-10-21 09:09:35

+0

也检查array1的数量 – 2014-10-21 09:15:57

0

您的自定义单元是从UITableViewCell继承的吗?你能告诉我们你的自定义单元代码吗?你也可以尝试:

CustomCell *cell = [tableView dequeuReusableCellWithIdentifier:@"YourCellIdentifier" forIndexPath:indexPath]; 
0

使用dequeueReusableCellWithIdentifier:forIndexPath:而不是dequeueReusableCellWithIdentifier:,如果你看一下文档,

的,

dequeueReusableCellWithIdentifier:forIndexPath 

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.

dequeueReusableCellWithIdentifier:

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil.

注意“这个方法返回nil”。

仅供参考,请不要忘记您在使用使用方法之前注册你的笔尖,registerNib:forCellReuseIdentifier:

由于您使用的故事板,确保“Resuse标识符”在故事板和一个你正在使用,以创建单元格是一样的。

+0

这不是导致问题的原因。如果OP用于出队的方法(旧的)返回nil,那么新的更长的方法也会(因为缺少或不正确的标识符)。鉴于OP的代码,唯一的区别是哪条线会导致崩溃。另外,OP说他正在使用故事板,所以你不应该在那种情况下注册一个笔尖。 – rdelmar 2014-10-20 16:16:11

+0

@rdelmar,你是对的。我错过了故事板部分。我只是提供了一些可能有助于OP的信息。 – akshaynhegde 2014-10-21 01:57:34

0

先回答解决了你的错误:从分镜检查小区标识,它应该是所在的标识符名称不能恢复ID

第二个答案:你需要把一个断点,你填写你的cell.lblTime.text和如果它是空的,请检查您的阵列填充位置,并尝试理解其为空的原因。随意填写以提出进一步的问题。

0

如果您已在故事板和电池也在它的表视图,那么我认为你可能不会设置在故事板的小区标识。

设置你的故事板电池“MainCell”的再利用标识