cellForRowAtIndexPath和过滤数据

时间:2013-11-18 10:59:29

标签: ios xml uitableview

我有一个我解析的xml文件,在那个xml文件中,我只需要在屏幕上显示一些精确数据并显示一次,因为xml中可以有多个数据声明。其余的xml数据必须保留在变量中供以后使用。我需要在方法cellForRowAtIndexPath中过滤NSXMLParser返回的NSMutableArrays中的数据。但是我的显示器里有白色的细胞。如何知道我无法从cellForRowAtIndexPath返回nil,摆脱白细胞?

这是我的代码(I J K L M是我的.h中的C类型整数):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier] autorelease];

    CGRect frame = CGRectMake(0, 0, 200, 50);
    UILabel *lbl1 = [[UILabel alloc] initWithFrame:frame];
    lbl1.textAlignment = NSTextAlignmentRight;
    [lbl1 setFont:[UIFont fontWithName:@"Helvetica" size:16.0]];
    [lbl1 setTextColor:[UIColor grayColor]];
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    lbl1.text = [[stories objectAtIndex: storyIndex] objectForKey: @"type"];
    NSLog(lbl1.text);
    if ([lbl1.text isEqualToString:@"MEASURES"] == YES && i == 0)
    {
        [cell.contentView addSubview:lbl1];
        i = 1;
        [lbl1 release];
        return cell;
    }else if ([lbl1.text isEqualToString:@"CAPTURES"] == YES && j == 0)
    {
        [cell.contentView addSubview:lbl1];
        j = 1;
        [lbl1 release];
        return cell;
    }else if ([lbl1.text isEqualToString:@"PDFREPORT"] == YES && k == 0)
    {
        [cell.contentView addSubview:lbl1];
        k = 1;
        return cell;
    }else if ([lbl1.text isEqualToString:@"VIDEO"] == YES && l == 0)
    {
        [cell.contentView addSubview:lbl1];
        l = 1;
        [lbl1 release];
        return cell;
    }else if ([lbl1.text isEqualToString:@"SIZINGSHEET"] == YES && l == 0)
    {
        [cell.contentView addSubview:lbl1];
        m = 1;
        [lbl1 release];
        return cell;
    }

    return cell;
}

我如何得到我的NSMutableArray:

重要变量:

NSMutableArray * stories; 
NSString * currentElement;
NSMutableString * currentData_id, * currentSizing_id, * currentType, * currentName, * currentSize;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"data"]) {
        item = [[NSMutableDictionary alloc] init];
        currentData_id = [[NSMutableString alloc] init];
        currentSizing_id = [[NSMutableString alloc] init];
        currentName = [[NSMutableString alloc] init];
        currentSize = [[NSMutableString alloc] init];
        currentType = [[NSMutableString alloc]init];
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"data"]) {
        [item setObject:currentData_id forKey:@"data_id"];
        [item setObject:currentSizing_id forKey:@"sizing_id"];
        [item setObject:currentName forKey:@"name"];
        [item setObject:currentSize forKey:@"size"];
        [item setObject:currentType forKey:@"type"];
        HERE I FILL IT, I THINK-->>>[stories addObject:[item copy]];
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"found characters in found characters: %@", string);

    if ([currentElement isEqualToString:@"data_id"]) {
        [currentData_id appendString:string];
    } else if ([currentElement isEqualToString:@"sizing_id"]) {
        [currentSizing_id appendString:string];
    } else if ([currentElement isEqualToString:@"name"]) {
        [currentName appendString:string];
    } else if ([currentElement isEqualToString:@"size"]) {
        [currentSize appendString:string];
    }else if ([currentElement isEqualToString:@"type"]) {
        [currentType appendString:string];
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];

    NSLog(@"all done!");
    NSLog(@"stories array has %d items", [stories count]);
    [newsTable reloadData];
}

1 个答案:

答案 0 :(得分:2)

我猜你通过解析XML文件来填充你的stories对象,但是你如何决定tableView中哪些元素需要一个单元?

通过使用两个数组,一个包含解析的总结果,另一个只包含需要一个单元格的元素,您将能够避免使用空白单元格。

按正确的顺序:

- Create your stories array by parsing the XML
- Create you 2nd array printableStories by reducing the first array the way you want
- Edit your numberOfRowsInSection method ( = [printableStories count])
- Edit your cellForRowAtIndexPath method (just replace stories with printableStories)

修改

以您希望的方式填充tableView以这种方式创建第二个数组:

NSMutableArray *yourSecondArray = [NSMutableArray arrayWithArray:stories];
for (NSDictionary *dico in yourSecondArray)
{
    // Here is your condition to NOT give the element a cell
    if ([dico objectForKey@"something"] == nil)
        [array removeObject:dico];
}