当空字段到达时,在iPhone中删除了分组表视图中的行?
-
24-10-2019 - |
题
我已经在分组的表视图中显示了数据。数据显示在XML解析中的表视图中。我有表视图的2个部分,一节有三行,第二部分有两行。
section 1 -> 3 Rows
section 2 - > 2 Rows.
现在,我想检查一下字符串的任何人是空的,那么我应该删除空单元格,因此我遇到了一些问题,如果我删除了任何空单元,那么它将更改索引号。那么,我该如何检查,任何人都为空?所以请给我任何示例代码或链接吗?我该如何实现?
示例代码,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
if([userEmail isEqualToString:@" "] || [phoneNumber isEqualToString:@" "] || [firstName isEqualToString:@" "])
{
return 2;
}
else {
return 3;
}
}
if (section == 1) {
if(![gradYear isEqualToString:@" "] || ![graduate isEqualToString:@" "]) {
return 1;
}
else
{
return 2;
}
return 0;
}
请帮助我!!!
谢谢。
解决方案
根据我的理解,您不想添加数据为空的行,因此请在告诉表查看部分和行之前,请建议您在截面数据上持续下去。
因此,可能会遵循代码可以帮助您...,我已经测试了它,您只需要从“ viewDidload”方法调用方法“准备data”,然后定义.h文件中的部分阵列即可。
- (void) prepareSectionData {
NSString *userEmail = @"";
NSString *phoneNumber = @"";
NSString *firstName = @"";
NSString *gradYear = @"";
NSString *graduate = @"";
sectionOneArray = [[NSMutableArray alloc] init];
[self isEmpty:userEmail]?:[sectionOneArray addObject:userEmail];
[self isEmpty:phoneNumber]?:[sectionOneArray addObject:phoneNumber];
[self isEmpty:firstName]?:[sectionOneArray addObject:firstName];
sectionTwoArray = [[NSMutableArray alloc] init];
[self isEmpty:gradYear]?:[sectionTwoArray addObject:gradYear];
[self isEmpty:graduate]?:[sectionTwoArray addObject:graduate];
}
-(BOOL) isEmpty :(NSString*)str{
if(str == nil || [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0)
return YES;
return NO;
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0){
return [sectionOneArray count];
} else if (section == 1) {
return [sectionTwoArray count];
}
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
if(indexPath.section == 0){
cell.textLabel.text = [sectionOneArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
cell.textLabel.text = [sectionTwoArray objectAtIndex:indexPath.row];
}
return cell;
}
其他提示
@Pugal Devan,好吧,您可以将数据保留在一个数组中,但是在这种情况下,您必须照顾数组界限并为不同的部分正确索引。对于每个部分,IndexPath.ROW将从索引0开始,如果您的数据在单个数组中,则必须通过自己管理行索引。但是,如果您想保留它,就可以做到:
int sectionOneIndex = 0;
int sectionTwoIndex = 3;
NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithObjects:@"email", @"Name", @"address", @"zipCode", @"country", nil];
上方两个整数代表您不同部分元素的起始位置。段数组中的前3个对象是第一部分的一部分,最后两个对象是第二部分的一部分。现在,您需要返回正确的行计数。为此,您可能会写:
if(section == 0) return [sectionArray count] - (sectionTwoIndex-1); //returns 3
else if(section == 1) return [sectionArray count] - sectionTwoIndex; //returns 2
或者,如果您的计数是静态的,则可以将恒定值放在回报中。
在您从数组中读取时,您只需在行值中添加此索引,该索引将返回当前单元格的正确位置。
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
if(indexPath.section == 0){
cell.textLabel.text = [sectionArray objectAtIndex:indexPath.row + sectionOneIndex];
} else if (indexPath.section == 1) {
cell.textLabel.text = [sectionArray objectAtIndex:indexPath.row + sectionTwoIndex];
}
return cell;
}
不隶属于 StackOverflow