如何从 UITableView 中删除不需要的单元格或行

How to remove unwanted Cells or Rows from UITableView

在我的应用程序中,我有两个 TableViewController,首先我使用收藏按钮随机选择了几行,并将它们的 indexPath.row 保存在单独的 MutableArray 中,然后我尝试仅访问在第二个 TableViewController 中选择的那些行,我使用了以下代码,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return rArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Radio * RadioObject;
    RadioObject = [rArray objectAtIndex:indexPath.row];
    if([rFavorites containsObject:[NSNumber numberWithInt:(int)indexPath.row]])
        cell.textLabel.text = RadioObject.radioTitle;
    else
        cell.frame = CGRectZero;


    return cell;

}

它给了我跟随结果。当从第一个 TableViewController 中选择三个单元格并将其结果存储在 MutableArray 中时,这里是它的 NSLog 结果

Favorite Radios array is (
    10,
    8,
    9
)

现在在模拟器中

]1

我愿意做的事情是从此 TableViewController 中删除其他空单元格。所以请建议我如何做到这一点?

试试这个代码。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return rFavorites.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

Radio * RadioObject;
RadioObject = [rArray objectAtIndex:[rFavorites[indexPath.row]]];
    cell.textLabel.text = RadioObject.radioTitle;


return cell;

}

实际上您做的是错误的,您应该为收藏夹创建新数组,因为稍后第一个数组可能会更新,并且由于您只有引用作为行号,第二个引用数组没有用。

要回答您的问题,当您没有参考编号时,只需将行高设置为 0...

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath      *)indexPath;
{
    if (don't have reference) return 0;
    return your_default_height
}

如果您想从表视图中删除所有其他行[仅显示选定的行]:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return rFavorites.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Radio * RadioObject;
    NSInteger index_rArray = [[NSString stringWithFormat:@"%@",[rFavorites objectAtIndex:indexPath.row]] integerValue];
    RadioObject = [rArray objectAtIndex:index_rArray];
    cell.textLabel.text = RadioObject.radioTitle;
    return cell;
}

如果您希望保留除 hide/compress 之外的所有行,那么;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
{
    if([rFavorites containsObject:[NSNumber numberWithInt:(int)indexPath.row]]){
    return 50; // whatever your cell size is
    }
    else {
    return 0;
    }
}

您是否尝试过完全不添加它们? :)

也许您可以在切换表格时切换源数组,这样当重新加载时调用 numberOfRows 时,tableView 将重新加载正确的数组,并显示正确数量的单元格。

那么您就不需要像将 cell.frame 设置为 CGZero 那样进行破解了。不过,如果您将 heightForRow 设置为 UITableViewAutomaticDimension,该 hack 可能会起作用。