2014-01-23 104 views
0

我想添加一个静态单元格作为我的动态tableview中的第一个单元格。我在这里看到了其他问题,但他们似乎没有工作。任何帮助是极大的赞赏。我实际上得到了我的单元格,但它取代了我的第一个动态单元格,当我在表格视图中滚动时,我的应用程序崩溃。在动态tableview中添加静态单元格

这是我在迄今为止已到达:

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


static NSString *CellIdentifier = @"Cell"; 
CustomSideBarCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (indexPath.row < NUMBER_OF_STATIC_CELLS) { 

cell.sidePic.image = _secondImage; 
cell.sideOption.text = @"Everything"; 

return cell; 

} 

else { 

cell.sidePic.image = _secondImage; 
_tempResults = [_tableData objectAtIndex:indexPath.row]; 
_optionCategory = [[_tempResults objectForKey:@"post"] objectForKey:@"category_name"]; 
cell.sideOption.text = _optionCategory; 
+0

您确切的问题是什么?你有错误信息吗? – Hannes

+0

就像我提到的,第一个问题是我的静态单元“覆盖”或替换第一个动态单元,而不是在它之上。第二个问题是当我尝试在tableview中滚动时,应用程序崩溃。 – mreynol

+0

这是抛出的错误:由于未捕获的异常'NSRangeException',理由:' - [__ NSCFArray objectAtIndex:]:index(14)超出界限(14)'' – mreynol

回答

3

我觉得你的问题是,你要访问您的_tableDataindexPath.row这在NUMBER_OF_STATIC_CELLS开始创建所有静态细胞后。

假设您有4个静态单元格和6个动态单元格。总共有10个电池。 因此,创建所有静态单元格后(indexPath.row = 0 - 3),即使您的dataSource(_tableData)只有6个元素,也会创建动态单元格为indexPath.row = 4 - 9。这就是为什么在访问_tableData时必须减去静态单元的数量。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    CustomSideBarCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    //static cell 
    if (indexPath.row < NUMBER_OF_STATIC_CELLS) { 
     cell.sidePic.image = _secondImage; 
     cell.sideOption.text = @"Everything"; 
     return cell; 
    } 

    //dynamic cell 
    cell.sidePic.image = _secondImage; 
    //subtract the number of static rows to start at 0 for your dataSource 
    _tempResults = [_tableData objectAtIndex:indexPath.row - NUMBER_OF_STATIC_CELLS]; 
    _optionCategory = [[_tempResults objectForKey:@"post"] objectForKey:@"category_name"]; 
    cell.sideOption.text = _optionCategory; 
    return cell; 
} 
+0

OMG的未捕获的异常终止!那样做了!我知道它必须是类似的东西,但不能将其转化为代码!非常感谢!! – mreynol

+0

哦,等我说得太快!有什么关。在视觉上,所有的细胞都显示...但是当我点击细节(基本上)的细节上移一个。 – mreynol

+0

你可以粘贴你的'didSelectRowAtIndexPath'方法的代码吗? – Hannes

相关问题