Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在UITableView中隐藏特定单元格_Objective C_Uitableview - Fatal编程技术网

Objective c 如何在UITableView中隐藏特定单元格

Objective c 如何在UITableView中隐藏特定单元格,objective-c,uitableview,Objective C,Uitableview,我从一个使用AFNetworking的MySQL数据库中获取数据,数据正在输入和输入 -(UITableView单元格*)tableView:(UITableView*)tableView单元格for行索引路径:(nsindepath*)indepath if ([creator isEqual: userID]) { [cell.contentView addSubview:imageView]; cell.textLabel.text = [[NSString alloc

我从一个使用AFNetworking的MySQL数据库中获取数据,数据正在输入和输入
-(UITableView单元格*)tableView:(UITableView*)tableView单元格for行索引路径:(nsindepath*)indepath

if ([creator isEqual: userID]) {

    [cell.contentView addSubview:imageView];

    cell.textLabel.text = [[NSString alloc] initWithFormat:@"       %@",[tempDictionary objectForKey:@"team_name"]];
    cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@        %@ \n%@", [tempDictionary objectForKey:@"date"], [tempDictionary objectForKey:@"time"], [tempDictionary objectForKey:@"location"]];

hideCells = NO;

} else {

    [imageView removeFromSuperview];

    cell.textLabel.text = nil;
    cell.detailTextLabel.text = nil;
hideCells = YES;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 if(indexPath.row == someCell && hideCells == YES)
return 0;
else
return 44;
}
我有代码来检查当前登录的用户是否等于MySQL中的字段“creator”,条目会显示出来,但我希望隐藏空单元格

if ([creator isEqual: userID]) {

    [cell.contentView addSubview:imageView];

    cell.textLabel.text = [[NSString alloc] initWithFormat:@"       %@",[tempDictionary objectForKey:@"team_name"]];
    cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@        %@ \n%@", [tempDictionary objectForKey:@"date"], [tempDictionary objectForKey:@"time"], [tempDictionary objectForKey:@"location"]];

} else {

    [imageView removeFromSuperview];

    cell.textLabel.text = nil;
    cell.detailTextLabel.text = nil;

}

UITableView只显示所选字段的数据,但也显示所有其他空单元格(因为它仍在计算数组)。

我建议您根据自己的条件(即
([creator isEqual:userID])从
主数组中准备另一个
数组
)在加载
tableView
和使用此
另一个数组加载
tableView
之前,唯一的方法是更新数据源数组,您还需要从数据源数组中删除该对象。如果您还想维护这些值,那么您可以再维护一个数组,您可以过滤该数组以获得所有具有值的对象,并使用谓词用过滤对象填充数据源数组。我希望这会对您有所帮助。

可以通过在
-(CGFloat)tableView:(UITableView*)tableView heightforrowatinexpath:(nsindepath*)indepath中将高度设置为零来隐藏单元格

第一个选项是设置一些用于隐藏空单元格的标志

(UITableViewCell*)表视图中:(UITableView*)表视图cellForRowAtIndexPath:(NSIndexPath*)indexPath

if ([creator isEqual: userID]) {

    [cell.contentView addSubview:imageView];

    cell.textLabel.text = [[NSString alloc] initWithFormat:@"       %@",[tempDictionary objectForKey:@"team_name"]];
    cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@        %@ \n%@", [tempDictionary objectForKey:@"date"], [tempDictionary objectForKey:@"time"], [tempDictionary objectForKey:@"location"]];

hideCells = NO;

} else {

    [imageView removeFromSuperview];

    cell.textLabel.text = nil;
    cell.detailTextLabel.text = nil;
hideCells = YES;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 if(indexPath.row == someCell && hideCells == YES)
return 0;
else
return 44;
}
第二种选择是在数据源字典中添加额外的键值对,以跟踪是否需要隐藏单元格

if ([creator isEqual: userID]) {

    [cell.contentView addSubview:imageView];

    cell.textLabel.text = [[NSString alloc] initWithFormat:@"       %@",[tempDictionary objectForKey:@"team_name"]];
    cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@        %@ \n%@", [tempDictionary objectForKey:@"date"], [tempDictionary objectForKey:@"time"], [tempDictionary objectForKey:@"location"]];

[array replaceObjectAtIndex:indexPath.row withObject:@"YES"];

} else {

    [imageView removeFromSuperview];

    cell.textLabel.text = nil;
    cell.detailTextLabel.text = nil;
[array replaceObjectAtIndex:indexPath.row withObject:@"YES"];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 if([[array objectAtIndex:indexPath.row]isEqualToString:@"NO"] )
return 0;
else
return 44;
}

我认为belo link可以解决您的问题。