iphone- UITableView只显示一个值

时间:2013-04-11 09:52:40

标签: iphone ios xcode

解析UITableView后,我的XML未显示所有值。它只返回一行。解析xml之后,为什么只显示一行而不是整个ArrayList?我检查了我的xml php页面。它显示所有价值都很好。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[xmlcont xmlbanktransfer] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSUInteger const kMagnitudeImageTag = 0;
    static NSUInteger const kLocationLabelTag = 4;
    static NSUInteger const klocationtag = 6;
    static NSUInteger const kacctag = 8;
    static NSUInteger const knameLabelTag = 9;


    UIImageView *magnitudeImage = nil;
    UILabel*locationLabel = nil;
    UILabel*locationtag=nil;
    UILabel*acc=nil;
    UILabel*nameLabel= nil;
    //NSMutableArray *cont = [xmlcont tweets];
    // Tweet *current       = [cont objectAtIndex:indexPath.row];
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        /*//common settings
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
         cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
         cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
         cell.imageView.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f);
         cell.imageView.clipsToBounds = YES;

         */
    }

    for(UIView *view in cell.contentView.subviews)
    {
        if ([view isKindOfClass:[UIView class]])
        {
            [view removeFromSuperview];
        }
    }
    NSMutableArray *cont = [xmlcont xmlbanktransfer];
    trans *current= [cont objectAtIndex:indexPath.row];
    NSLog(@"sd %@",current.date);
     NSLog(@"sd %@",current.name);
    locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,10, 250, 20)];
    locationLabel.tag = kLocationLabelTag;
    locationLabel.font = [UIFont boldSystemFontOfSize:15];
    [cell.contentView addSubview:locationLabel];

    locationLabel.text=current.name;
    locationLabel.textColor=[UIColor colorWithRed:0.050 green:0.278 blue:0.392 alpha:1];
    //locationLabel.backgroundColor=[UIColor lightGrayColor];
    locationLabel.backgroundColor=[UIColor colorWithRed:225 green:225 blue:225 alpha:0] ;
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(180,10, 250, 20)];
    nameLabel.tag = knameLabelTag;
    nameLabel.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:nameLabel];
    nameLabel.text=current.date ;
    nameLabel.textColor=[UIColor colorWithRed:0.050 green:0.278 blue:0.392 alpha:1];
    //locationLabel.backgroundColor=[UIColor lightGrayColor];
    nameLabel.backgroundColor=[UIColor colorWithRed:225 green:225 blue:225 alpha:0] ;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

解析xml的代码:

    xmlbanktransfer=[[NSMutableArray alloc] init];
    NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];


    parser=[[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser parse];
    NSLog(@"testing12 %@ ",url);
   // NSLog(@"tweets %@ ", xmlbanktransfer);
    NSLog(@"Total values = %d",[xmlbanktransfer count]);
    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{

    if([elementName isEqualToString:@"item"])
    {
        currentTweet       = [trans alloc];
        banktransNodeContent =[[NSMutableString alloc] init];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

    if([elementName isEqualToString:@"date"])
    {

        currentTweet.date = [currentTweet.date stringByReplacingOccurrencesOfString:@"&" withString:@" "];
        currentTweet.date = banktransNodeContent;



    }

    if([elementName isEqualToString:@"name"])
    {
        currentTweet.name =banktransNodeContent;


    }
    if ([elementName isEqualToString:@"type"])
    {
        currentTweet.type=banktransNodeContent;
    }

    if ([elementName isEqualToString:@"gross"])
    {
        currentTweet.gross=banktransNodeContent;
    }


    if ([elementName isEqualToString:@"fee"])
    {
        currentTweet.fee=banktransNodeContent;
    }

    if ([elementName isEqualToString:@"net"])
    {
        currentTweet.net=banktransNodeContent;
        NSLog(@"current tweet  %@",currentTweet.net);
    }

    if ([elementName isEqualToString:@"category"])
    {
        currentTweet.category=banktransNodeContent;
        NSLog(@"current tweet  %@",currentTweet.category);
    }

    if ([elementName isEqualToString:@"account"])
    {
        currentTweet.account=banktransNodeContent;
        NSLog(@"current tweet  %@",currentTweet.account);
    }

    if([elementName isEqualToString:@"item"])
    {

        [xmlbanktransfer addObject:currentTweet];

        currentTweet = nil;

        banktransNodeContent = nil;
    }

}




- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string


{

    NSLog(@"currentNodeContent are %@",banktransNodeContent);
    banktransNodeContent = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSLog(@"currentNodeContentstring test are %@",banktransNodeContent);

}

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    banktransNodeContent = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}

2 个答案:

答案 0 :(得分:0)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

此委托函数告诉数据源返回表视图的给定部分中的行数。 如果你有一个数组中的对象(当你问它1时)应该只创建1个UITableViewCell。 好像你接受了更多,parsing XML应该存在一些问题.Xmls和Parsing技术与开发人员开发人员不同。因此,请逐步尝试yu出错的地方。我可以为学习Xml parsing in iPhone

提供简单的链接

答案 1 :(得分:0)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[xmlcont xmlbanktransfer] count];
}

此方法告诉tableview要填充多少个单元格

记录该值并查看返回的数量[现在必须返回1,因为该表显示了一行]。更正返回值并显示所有值

要检查的事项

  • 上述回归价值
  • 确保数组xmlbanktransfer是一个强类型的属性