iOS中tableview中的多单元标识符
我正在使用一个数组,其中包含来自两个字典的对象,并在第一个单元格上显示第一个字典的对象,该单元格具有不同的标识符,并且与第二个字典对象相同 在cellforrowatinexpath中查看我的代码iOS中tableview中的多单元标识符,ios,objective-c,uitableview,dictionary,Ios,Objective C,Uitableview,Dictionary,我正在使用一个数组,其中包含来自两个字典的对象,并在第一个单元格上显示第一个字典的对象,该单元格具有不同的标识符,并且与第二个字典对象相同 在cellforrowatinexpath中查看我的代码 static NSString *cellIdentifier = @"cellIdentifier1"; static NSString *customCellIdentifier = @"cellIdentifiercustom"; if (indexPath.row == 0)
static NSString *cellIdentifier = @"cellIdentifier1";
static NSString *customCellIdentifier = @"cellIdentifiercustom";
if (indexPath.row == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
ExcName = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 2.0, 250.0, 36.0)];
ExcName.font = [UIFont systemFontOfSize:12.0];
ExcName.textColor = [UIColor blackColor];
ExcName.textAlignment = NSTextAlignmentLeft;
ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"activityname"];
ExcName.numberOfLines=3;
[cell.contentView addSubview:ExcName];
return cell;
}
else{
UITableViewCell *customcell = [tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
// if (cell==nil)
{
customcell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:customCellIdentifier];
}
customcell.selectionStyle = UITableViewCellSelectionStyleNone;
ExcName = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 2.0, 250.0, 36.0)];
ExcName.font = [UIFont systemFontOfSize:12.0];
ExcName.textColor = [UIColor blackColor];
ExcName.textAlignment = NSTextAlignmentLeft;
ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"exercisename"];
// ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"exercisename"];
ExcName.numberOfLines=3;
[customcell.contentView addSubview:ExcName];
return customcell;
}
return nil;
}
现在,我想知道,如果任何字典为空,则对应于该字典的单元格不应可见,而该字典现在可见。以下几点:
-tableView:cellforrowatinexpath:
返回nil
。对于每个(可见)行,该方法只被调用一次,并且该表假设它的行数与您通过返回值-tableView:numberOfRowsInSection:
所告诉的行数相同。默认为nil
会显示代码中存在不一致
-dequeueReusableCellWithIdentifier:forRowAtIndexPath:
。当重用池中没有单元时,它负责为您分配新单元
解决方案: 不要“跳过行”,而是使数据模型与要显示的内容保持一致。如果您有一个包含某些要跳过的对象的数组,请先筛选数组,然后将筛选后的数组用作数据源
// Call this method once before reloading the table view
- (void) filterDictionaries
{
// (filteredArray is an ivar defined as NSMutableArray*)
for (NSDictionary* dictionary in exerciseAr) {
if ([dictionary objectForKey:@"exercisename"] != nil) {
[filteredArray addObject: dictionary];
}
}
// Now you can use filteredArray as the data source
// instead of exerciseAr, without worrying about skipping
// entries.
}
- (NSInteger) tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger) section
{
return [filteredArray count];
}
- (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
static NSString *cellIdentifier = @"cellIdentifier1";
static NSString *customCellIdentifier = @"cellIdentifiercustom";
UITableViewCell* cell;
if (indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forRowAtIndexPath:indexPath];
}
else{
cell = [tableView dequeueReusableCellWithIdentifier:customCellIdentifier forRowAtIndexPath:indexPath];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.excLabel.text = [[filteredArray objectAtIndex:indexPath.row]
return cell;
}
有几件事:
-tableView:cellforrowatinexpath:
返回nil
。对于每个(可见)行,该方法只被调用一次,并且该表假设它的行数与您通过返回值-tableView:numberOfRowsInSection:
所告诉的行数相同。默认为nil
会显示代码中存在不一致
-dequeueReusableCellWithIdentifier:forRowAtIndexPath:
。当重用池中没有单元时,它负责为您分配新单元
解决方案: 不要“跳过行”,而是使数据模型与要显示的内容保持一致。如果您有一个包含某些要跳过的对象的数组,请先筛选数组,然后将筛选后的数组用作数据源
// Call this method once before reloading the table view
- (void) filterDictionaries
{
// (filteredArray is an ivar defined as NSMutableArray*)
for (NSDictionary* dictionary in exerciseAr) {
if ([dictionary objectForKey:@"exercisename"] != nil) {
[filteredArray addObject: dictionary];
}
}
// Now you can use filteredArray as the data source
// instead of exerciseAr, without worrying about skipping
// entries.
}
- (NSInteger) tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger) section
{
return [filteredArray count];
}
- (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
static NSString *cellIdentifier = @"cellIdentifier1";
static NSString *customCellIdentifier = @"cellIdentifiercustom";
UITableViewCell* cell;
if (indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forRowAtIndexPath:indexPath];
}
else{
cell = [tableView dequeueReusableCellWithIdentifier:customCellIdentifier forRowAtIndexPath:indexPath];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.excLabel.text = [[filteredArray objectAtIndex:indexPath.row]
return cell;
}
这实际上取决于你所说的字典为空是什么意思。它可能是nil,或者只是没有任何键的值,例如键的值是空字符串。因此,为了补充NibolasMiari的答案,我想说,检查这两种情况
- (void) filterDictionaries:(NSArray*) exerciseAr{
filteredArray = [[NSMutableArray alloc]init];
for (NSDictionary* myDict in exerciseAr) {
if ([myDict isKindOfClass:[NSNull class]]) {
continue;
}
else if ([myDict count] == 0) {
continue;
}
else{
[filteredArray addObject: myDict];
}
}
}
- (NSInteger) tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger) section
{
return [filteredArray count];
}
现在,在您的cellforIndexPath
方法中,所有内容都将是相同的,只需按以下方式更改行即可-
在if区,你有
ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"activityname"];
成功-
ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"activityname"];
ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"exercisename"];
在else街区,你有
ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"exercisename"];
成功-
ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"activityname"];
ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"exercisename"];
这实际上取决于你所说的字典为空是什么意思。它可能是nil,或者只是没有任何键的值,例如键的值是空字符串。因此,为了补充NibolasMiari的答案,我想说,检查这两种情况
- (void) filterDictionaries:(NSArray*) exerciseAr{
filteredArray = [[NSMutableArray alloc]init];
for (NSDictionary* myDict in exerciseAr) {
if ([myDict isKindOfClass:[NSNull class]]) {
continue;
}
else if ([myDict count] == 0) {
continue;
}
else{
[filteredArray addObject: myDict];
}
}
}
- (NSInteger) tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger) section
{
return [filteredArray count];
}
现在,在您的cellforIndexPath
方法中,所有内容都将是相同的,只需按以下方式更改行即可-
在if区,你有
ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"activityname"];
成功-
ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"activityname"];
ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"exercisename"];
在else街区,你有
ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"exercisename"];
成功-
ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"activityname"];
ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"exercisename"];
如果特定字典在heightforrowatindexpath委托方法中为空,则可以将高度更改为0。然后,您应该将该逻辑放入
-tableView:numberOfRowsInSection:
@Jaimish中,这是不推荐的(运行时会抱怨)。不要折叠单元格,而是告诉表视图需要显示的实际行数,并每次返回一个完全配置的单元格-tableView:CellForRowatineXpath:`s调用。@NicolasMiari您可以解释一下,通过编码pleaseok然后@NavjotSingh,请向我显示您的NumberOfRowInstitution方法如果HeightForRowatineXpath委托方法中的特定字典为空,您可以将高度更改为0,然后,您应该将该逻辑放在-tableView:numberofrowsinssection:
@Jaimish中,这是不推荐的(运行时会抱怨)。不要折叠单元格,而是告诉表视图需要显示的实际行数,每次都返回一个完全配置的单元格-tableView:CellForRowatineXpath:`Iscalled.@NicolasMiari您能解释一下,通过编码请确定然后@NavjotSingh请向我显示您的NumberOfRowInstitution方法请先理解我的问题您的意思是这部分:“现在,我想知道,如果任何字典为空,则与该字典对应的单元格不应可见,而该字典现在是可见的。”是的,那么我如何对其进行筛选以使筛选后的数组显示我的更新答案。我在代码中明确了一些仅在文本中提及的内容。请先理解我的问题。你是指这部分:”现在我想知道,如果任何字典为空,那么对应于该字典的单元格就不应该是可见的,因为它现在是可见的。“是的,那么我如何过滤它以使过滤后的数组显示我更新的答案。我在代码中明确了一些仅在文本中提到的内容。