【问题标题】:How to remove unwanted Cells or Rows from UITableView如何从 UITableView 中删除不需要的单元格或行
【发布时间】:2017-03-11 10:55:45
【问题描述】:

在我的应用程序中,我有两个 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 中删除其他空单元格。所以请建议我如何做到这一点?

【问题讨论】:

  • 你能 NSLOG rArray 告诉我们结果吗?
  • @Anbu.Karthik 这不像你那么容易,实际上我只需要在包含超过 150 行的第一个 tableViewController 中显示用户选择的行或单元格,并且当用户收藏几行时它只需要对可变数组中的那些行的引用编号,而不是所有单元格内容&当希望转到第二个视图控制器时,它应该只显示那些用户选择的行或单元格,我愿意删除上面所有不需要的或空的单元格& 以及波纹管或在那些最喜欢的单元格之间。我希望它会更好地阐明这个想法,
  • @Anbu.Karthik 我已经探索了许多链接并尝试了您最近添加到您的评论中的链接,实际上我从前两天开始就在做同样的工作。想想到目前为止我会探索和尝试多少个链接。如果您能解决这个问题,请返回您的代码建议。
  • 在第二个表格视图控制器中,numberOfRowsInSection 应该返回rFavorites.count。然后在cellForRowAt 中获取rFavorites[indexPath.row] 的值并使用它来索引rArray。您可能想要对rFavorites 进行排序

标签: ios objective-c uitableview nsmutablearray


【解决方案1】:

如果要从 tableview 中删除所有其他行 [仅显示选定的行]:

- (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;
}

如果您希望保留所有行但隐藏/压缩然后;

- (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;
    }
}

【讨论】:

  • return 0; 不工作。这就是我要发布问题的主要问题。否则....好吧,让我试试你的第一个建议,一会儿再回来。
  • 即使我不知道,但只需尝试返回 1; :|
  • 你的第一个方法的后续行 NSInteger index_rArray = [rFavorites objectAtIndex:indexPath.row]; 给出以下问题 incompatible pointer to integer conversion NSInteger (AKA Long) with an expression of type id 所以知道为什么它会给出这个问题吗???
  • ok.. 替换为 [[NSNumber numberWithInt:[rFavorites objectAtIndex:indexPath.row] intValue];
  • 这里要清楚地提一下,我的rFavorites 数组在其indexpath.row 中包含NSNumber 对象
【解决方案2】:

试试这个代码。

- (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;

}

【讨论】:

  • 下一行 RadioObject = [rArray objectAtIndex:[rFavorites[indexPath.row]]]; 给出错误 Expected Identifier
  • 这里你差不多有正确答案了,只不过rFavorites中包含NSNumbers,所以不能直接作为数组索引;您需要获取 NSNumber 并获取它的 intValue
【解决方案3】:

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

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

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

【讨论】:

  • 先生能否详细说明方法?
  • @HinaKhan :将您的项目上传到某个地方,让我们研究一下...必须返回 0...这是您不正确的解决方案的唯一问题...
【解决方案4】:

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

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

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

【讨论】:

  • 为什么?我可能遗漏了一些东西,你能解释一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多