从tableview中删除空值的方法?

时间:2012-08-13 05:29:21

标签: objective-c ios uitableview nsarray xcode4.3

新目标c。我正在使用NSArray填充UITableView。有没有办法从数组和cell.textLabel.text中删除空白。这就是我所做的:

- (void)viewDidLoad
{
    [super viewDidLoad];
    alTableView.delegate = self;
    alTableView.dataSource = self;
    NSArray *BusRoute = alightDesc;
    int i;
    int count = [BusRoute count];
    for (i = 0; i < count; i++)
    {  
        NSDictionary *STEPS = [dic valueForKey:@"STEPS"];            
        alDescArray = [[STEPS valueForKey:@"AlightDesc"] retain];
        boardDescArray = [[STEPS valueForKey:@"BoardDesc"] retain];   
        NSLog(@"boardDescArray = %@", boardDescArray);     

        serviceID = [[STEPS valueForKey:@"ServiceID"] retain];
    }
}

- (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];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:14]; //Change this value to adjust size
    cell.textLabel.numberOfLines = 10;
    cell.textLabel.text = [NSString stringWithFormat:@"Walk to and board at %@\nTake Bus Service = %@\nAlight Destination = %@",[boardDescArray objectAtIndex:indexPath.row], [serviceID objectAtIndex:indexPath.row], [alDescArray objectAtIndex:indexPath.row]];
    NSLog(@"cell = %@", cell.textLabel.text);
    return cell;
}

在第二个单元格打印出来时,走到(Null)并登机,是否有办法从表中删除该句子?

结果:

alDescArray = (
"OPP",
"OPP",
"OPP"
)

boardDescArray = (
"BLK",
"AFT",
""
)

serviceID = (
66,
43,
72
)

cell = Walk to and board at BLK 7
       Take Bus Service = 11
       Alight Destination = BLK 11
cell = Walk to and board at 
       Take Bus Service = 3
       Alight Destination = BLK 16
cell = Walk to and board at BLK 38 
       Take Bus Service = 9
       Alight Destination = AA

更新Ans

- (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];
}
cell.textLabel.font = [UIFont systemFontOfSize:14]; //Change this value to adjust size
cell.textLabel.numberOfLines = 10;

NSString *boardDesc = [boardDescArray objectAtIndex:indexPath.row];
NSString *serviceId = [serviceID objectAtIndex:indexPath.row];
NSString *alDesc = [alDescArray objectAtIndex:indexPath.row];
NSLog(@"serviceId - %@",serviceId);

NSString *resultString2 = [NSString stringWithString:@""];

//make a check before making the string
if ([boardDesc length]>0) {
    resultString2 =[resultString2 stringByAppendingFormat:@"Walk to and board at %@,",boardDesc];
}

if ([serviceId length]>0) {
    resultString2 =[resultString2 stringByAppendingFormat:@"Take Bus Service %@: ",serviceId];
}

if ([alDesc length]>0) {
    resultString2 =[resultString2 stringByAppendingFormat:@"Alight Destination %@: ",alDesc];
}

//string that doesnot print null values
NSLog(@"resultString2 - %@",resultString2);
cell.textLabel.text = resultString2;
NSLog(@"cell = %@", cell.textLabel.text);
return cell;
}

2 个答案:

答案 0 :(得分:2)

NSString *myName = [NSString stringWithString:@"Mangesh"];
NSString *myAge = [NSString stringWithString:@""];
NSString *myProfession = [NSString stringWithString:@"Developer"];
//just for example you have three string with above values 
//and you want to print Hi, My Name is ABC, I am 22 year old and my Profession is Developer

//string that prints null values     
NSString *resultString1 = [NSString stringWithFormat:@"Hi, My Name is %@, I am %@ year old and my profession is %@",myName,myAge,myProfession];

NSLog(@"resultString1 - %@",resultString1);
//output: resultString1 - Hi, My Name is Mangesh, I am  year old and my profession is Developer

NSString *resultString2 = [NSString stringWithString:@"Hi,"];

//make a check before making the string
if ([myName length]>0) {
    resultString2 =[resultString2 stringByAppendingFormat:@"My Name is %@,",myName];
}

if ([myAge length]>0) {
    resultString2 =[resultString2 stringByAppendingFormat:@"I am %@ year old and",myAge];
}

if ([myProfession length]>0) {
    resultString2 =[resultString2 stringByAppendingFormat:@" my profession is %@",myProfession];
}

//string that doesnot print null values
NSLog(@"resultString2 - %@",resultString2);
//output:-  resultString2 - Hi,My Name is Mangesh, my profession is Developer

答案 1 :(得分:0)

NSString *board = [boardDescArray objectAtIndex:indexPath.row];
NSString *firstline = [board length] ? @"Walk to and board at" : @"";

cell.textLabel.text = [NSString stringWithFormat:@"%@%@\nTake Bus Service =
%@\nAlight Destination = %@", firstline, board, [serviceID
objectAtIndex:indexPath.row], [alDescArray objectAtIndex:indexPath.row]];