Objective c UITableView只有一个字段

Objective c UITableView只有一个字段,objective-c,uitableview,Objective C,Uitableview,我尝试加载一个数组并在tableview上显示数组项。 我的tableview只显示web服务的一个字段 e、 g如果branchname是ABC。表视图显示40 ABC行 Tableview有40个相同的项目。我想加载所有40个项目 我的代码: 我的阵列 NSMutableArray *myArray; 解析XML代码 -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespac

我尝试加载一个数组并在tableview上显示数组项。 我的tableview只显示web服务的一个字段

e、 g如果branchname是ABC。表视图显示40 ABC行 Tableview有40个相同的项目。我想加载所有40个项目

我的代码:

我的阵列

NSMutableArray *myArray;
解析XML代码

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:    (NSDictionary *)attributeDict
{
    if ( [elementName isEqualToString:@"Branchnames"] ) {
        if (!retornoSOAP) {
        myArray = [[NSMutableArray alloc] init];
        }
    teveRetorno = YES;
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if (teveRetorno) {
        [myArray addObject:string];
        NSLog(@"My Array %@",myArray);
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ( [elementName isEqualToString:@"Branchnames"] ) {
        NSLog(@"My Array %@",myArray);
       [[self tableView]reloadData];
       retornoSOAP = nil;
       teveRetorno = NO;
    }
}
表代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

   return 40;
}

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

    // Return the number of rows in the section.
    return [self.myArray count];
}

- (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];
   }
 cell.textLabel.text =[myArray objectAtIndex:indexPath.row];
 return cell;
}

问题在于这一行:

myArray = [[NSMutableArray alloc] init];
每次出现
Branchnames
标记时,您都在分配数组。因此,数组将丢失以前的内容。将只插入最后一个对象

实施以下方法:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:    (NSDictionary *)attributeDict
{
   if ( [elementName isEqualToString:@"Branchnames"] )
   {
     teveRetorno = YES;
   }
   else if ([elementName isEqualToString:@"yourTopLevelXMLTag"]) //yourTopLevelXMLTag = ArizalarGetirResult
   {
     myArray = [[NSMutableArray alloc] init];
   }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
  if (teveRetorno)
  {

    [myArray addObject:string];

  }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
  if ( [elementName isEqualToString:@"Branchnames"] )
  {
    teveRetorno = NO;
  }
  else if ([elementName isEqualToString:@"yourTopLevelXMLTag"]) //yourTopLevelXMLTag = ArizalarGetirResult
  {
    [[self tableView]reloadData];
  }
}

对于我的数据模型,我通常创建一个向XMLParser和ViewController公开的单例,以便在解析XML时

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"rootElement"])
    {
        currElement = [[mySingleton alloc] init];
        currentElement.currentElementAttribute = [attributeDict objectForKey:@"type"];
    }
    else if ([elementName isEqualToString:@"somethingElse"])
    {
       //don't need to init another currElement here
    }
  }
我可以用ViewController中的数据做任何我想做的事情

self.myLabel.text = currElement.currentElementAttribute;
当然,这在使用NSMutableDictionary或NSArray时也会起作用

+ (NSMutableDictionary *)mySingletonDictionary
{
    static NSMutableDictionary *singletonInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singletonInstance = [[NSMutableDictionary alloc]init];
    });
    return singletonInstance;
}

这是一种非常安全的方法,可以实例化只能实例化一次的单例。

能否显示NSLog的结果(@“My Array%@”,myArray);解析后?你能显示myArray的NSLog输出吗?为什么给你40个部分?是否有必要?将NSLog array.count放入numberOfRows中method@MehmetAkyel:这40个部分需要什么?只需从该方法返回1并检查,有多少个元素。如果只有一行,那么您的数组只有一个元素,否则您的上述注释将不正确(没有40个相同的项。我在日志中看到其他结果)@MehmetAkyel:parserDidStartDocument正在调用吗?您能发布您的xml结构吗?让我们来看看