简体   繁体   English

IOS - cellForRowAtIndexPath跳过行

[英]IOS - cellForRowAtIndexPath skip row

Basically, the cellForRowAtIndexPath function need to return a UITableViewCell. 基本上, cellForRowAtIndexPath函数需要返回一个UITableViewCell。 In my code, I would like to check a behaviour that will check something and skip the cell if a specific value is found. 在我的代码中,我想检查一个将检查某些内容的行为,并在找到特定值时跳过单元格。

Here's what I have right now : 这就是我现在所拥有的:

static NSString *FirstCellIdentifier = @"First Custom Cell";
static NSString *SecondCellIdentifier = @"Second Custom Cell";

CustomObject *o = [_customObjects objectAtIndex:indexPath.row];

if ([s.name isEqualToString:@""])
{
    FirstCellController *cell = [customList dequeueReusableCellWithIdentifier:FirstCellIdentifier];
    if (!cell) { 
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FirstCustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (FirstCellController *) currentObject;
                break;
            }
        }
    }
    // Here I do something with the cell's content
    return cell;
}
else {
    SecondCellController *cell = [customList dequeueReusableCellWithIdentifier:SecondCellIdentifier];
    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SecondCustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (SecondCellController *) currentObject;
                break;
            }
        }
    }
    // Here i do something with the cell's content
    return cell;
}

What I'd like to do is that if the s.name is not empty, I would like to "skip" the cell, not displaying it and go to the next one. 我想要做的是,如果s.name不为空,我想“跳过”单元格,而不是显示它并转到下一个单元格。

Anyone have some advice on this please ? 有人对此有一些建议吗?

Thanks. 谢谢。

You cannot "skip" a cell this way. 你不能以这种方式“跳过”一个单元格。 If your datasource claims there are n rows, then you have to provide a cell for each of them. 如果您的数据源声明有n行,那么您必须为每个行提供一个单元格。 The correct way would be to modify your datasource to claim (n-1) rows when you want to remove one, then call UITableView reloadData to have it regenerate the table (and ask you for new cells for each visible row). 当你想要删除一个行时,正确的方法是将你的数据源修改为声明(n-1)行,然后调用UITableView reloadData让它重新生成表(并要求你为每个可见行提供新的单元格)。

Another option would be to "hide" a row/cell. 另一种选择是“隐藏”行/单元格。 The technique I've used for this is to provide a height of 0 for the given cell via heightForRowAtIndexPath : 我用于此的技术是通过heightForRowAtIndexPath为给定单元格提供0的高度:

Like tom said, don't try to "skip" in the UITableViewDelegate methods but instead put this logic in the UITableViewDataSource methods ... As an example, you can set up a common method to filter out your data: 就像汤姆所说的那样,不要试图在UITableViewDelegate方法中“跳过”,而是将这个逻辑放在UITableViewDataSource方法中...例如,你可以设置一个通用的方法来过滤你的数据:

- (NSArray*)tableData {
    NSMutableArray *displayableObjects = [NSMutableArray array];
    [displayableObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        CustomObject *customObject = (YourCustomObject *)obj;
        if (customObject.name && ![customObject.name isEqualToString:@""]) {
            // only show in the table if name is populated
            [displayableObjects addObject:customObject];
        }
    }];
    return displayableObjects;
}

and then in your datasource methods, use that to get the pre-filtered data that you want displayed in the table: 然后在您的数据源方法中,使用它来获取您希望在表中显示的预过滤数据:

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

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

    CustomObject *o = [[self tableData] objectAtIndex:indexPath.row];
    ....
}

This way whenever reloadData is called it will always skip the filtered data when building the cells. 这样,每当调用reloadData时,它将始终在构建单元格时跳过过滤后的数据。

You can Skip a cell by not showing it in the table view. 您可以通过不在表视图中显示单元来跳过单元格。 (I Assume thats what u want) Just use this: (我假设你想要的东西)只需使用这个:

cell.hidden = YES;
return cell

EDIT: Not a valid Solution. 编辑:不是有效的解决方案。 (Leaves Blank space in place of the cell) (留下空白区代替细胞)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS如何获取,当前在cellForRowAtIndexPath方法中选择了indexpath行? - iOS how to get, current selected indexpath row on cellForRowAtIndexPath method? insertRowsAtIndexPaths为每一行调用cellForRowAtIndexPath - insertRowsAtIndexPaths calling cellForRowAtIndexPath for every row iOS:在numberOfRowsInSection之后没有立即调用cellForRowAtIndexPath - iOS: cellForRowAtIndexPath not being called immediately after numberOfRowsInSection 如何将动态行和节数组传递给 numberOfRowsInSection 和 cellForRowAtIndexPath - How To Pass Dynamic row and Section Array to numberOfRowsInSection and cellForRowAtIndexPath UITableView iPhone中的cellForRowAtIndexPath方法出现问题(indexPath.row为空) - problem with cellForRowAtIndexPath method in UITableView iphone (indexPath.row is null) cellForRowAtIndexPath:尝试访问数组objectAtIndex:indexPath.row时崩溃 - cellForRowAtIndexPath: crashes when trying to access arrays objectAtIndex:indexPath.row 我在cellForRowAtIndexpath中添加了selectRowAtIndexPath来标记上一个选定的行但是如果滚动tablview它崩溃了 - I added selectRowAtIndexPath in cellForRowAtIndexpath to mark previous selected row but if scroll tablview it crashs UITable cellForRowAtIndexPath - UITable cellForRowAtIndexPath cellForRowAtIndexPath中崩溃 - Crash in cellForRowAtIndexPath cellForRowAtIndexPath的小计 - Subtotal at cellForRowAtIndexPath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM