如何通过scrollview创建动态标签显示?

时间:2013-07-16 11:48:29

标签: ios objective-c uiscrollview uilabel

我尝试通过选择计数在某些标签上显示网络服务字段。很抱歉,我的问题很大,但我必须解决这个问题。

我有一些标签。

选择" 1"分支

                       Branch Name : XYZ Branch


                        Product1   Product2   Product3  Product4



   Sales Number        12        13         15           22


  Liters               123       132        111          231

  Price              123000     1243300    1929922       2232233

选择" 2"分支

                       Branch Name : ABC Branch


                        Product1   Product2   Product3    Product4



   Sales Number        17           18             17      44  


  Liters               123          132            111     343

  Price                45454545     565656     34343434   2232323

输出在这里

选择前1分支时

enter image description here

选择前2分支时

enter image description here

我的问题:它一个接一个地向我展示。当我选择top 1.branch时,它会显示我的顶级1.branch细节。我希望当我从计数中选择前10名时,它将通过滚动视图(例如tableview)显示来自web服务的前10个分支详细信息。

      @interface ViewController ()

      {



   NSMutableData *webData;
   NSXMLParser *xmlParser;
   NSMutableString *retornoSOAP;
   BOOL teveRetorno;
   NSMutableString *retornoSOAP1;
   BOOL teveRetorno1;


  }

  - (void)selectAnElection:(NSNumber *)selectedIndex element:(id)element;
  - (void)selectProducts:(NSNumber *)selectedIndex element:(id)element;
  - (void)selectCount:(NSNumber *)selectedIndex element:(id)element;

   @end

   @synthesize elections = _elections;
   @synthesize products = _products;
  @synthesize topnumber = _topnumber;
  -(void)viewDidLoad
  {

  NSMutableArray* topnumbershow = [NSMutableArray new];

  self.elections= [NSArray arrayWithObjects:@"miktar", @"tutar", @"adet", nil];

  self.products = [NSArray arrayWithObjects:@"KB95", @"Motorin", @"LPG",@"Toplam", nil];

   for (int i = 1; i <= 50; i++) {
    [topnumbershow addObject:[NSString stringWithFormat:@"%d", i]];
   }

   self.topnumber= topnumbershow;

我的选择器按钮

   - (IBAction)selectAnElection:(UIControl *)sender {
  [ActionSheetStringPicker showPickerWithTitle:@"Choose a  election" rows:self.elections   
  initialSelection:self.selectedIndex target:self    
   successAction:@selector(selectAnElection:element:)   
   cancelAction:@selector(actionPickerCancelled:) origin:sender];

   }


  - (IBAction)selectProducts:(UIControl *)sender {
   [ActionSheetStringPicker showPickerWithTitle:@"Select a Products" rows:self.products 
   initialSelection:self.selectedIndex target:self 
   successAction:@selector(selectProducts:element:) 
    cancelAction:@selector(actionPickerCancelled:) origin:sender];
   }

  - (IBAction)selectCount:(UIControl *)sender {
  [ActionSheetStringPicker showPickerWithTitle:@"Top numbers ? " rows:self.topnumber
   initialSelection:self.selectedIndex target:self 
   successAction:@selector(selectCount:element:) 
   cancelAction:@selector(actionPickerCancelled:) origin:sender];

   }

显示详细信息按钮

  -(IBAction) ShowDetails:(UIButton *)sender


  {
     NSString *mensagemSOAP= [NSString stringWithFormat:@"<?xml version=\"1.0\" 

     encoding=\"utf-8\"?>\n"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-  
     instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"   
      xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
       "<soap:Body>\n"
        "<TopBranches xmlns=\"http://tempuri.org/\">\n"
        "<date1>%@</date1>\n"
        "<date2>%@</date2>\n"
        "<Elections>%@</Elections>\n"
        "<Products>%@</Products>\n"
        "<Count>%@</Count>\n"
         "</TopBranches>\n"
          "</soap:Body>\n"
           "</soap:Envelope>\n",
         Date1.text,Date2.text,_ElectionsTextField.text,
      _ProductsTextfield.text,_CountTextField.text];




      NSLog(@"SOAP Message = \n%@\n\n", mensagemSOAP);

NSURL *url = [NSURL URLWithString:@"http://webservice:1249/topbranchdetails.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];       
    NSString *tamanhoMensagem = [NSString stringWithFormat:@"%d", [mensagemSOAP length]];

   [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
   [theRequest addValue: @"http://tempuri.org/TopBranches"  
   forHTTPHeaderField:@"SOAPAction"];
   [theRequest addValue:tamanhoMensagem forHTTPHeaderField:@"Content-Length"];
   [theRequest setHTTPMethod:@"POST"];
   [theRequest setHTTPBody:[mensagemSOAP dataUsingEncoding:NSUTF8StringEncoding]];

     NSURLConnection *conexao = [[NSURLConnection alloc] initWithRequest:theRequest 
     delegate:self];

  if(conexao){
    webData = [NSMutableData data];
   }else{
    NSLog(@"Connection Error.");
   }
   }

XML解析过程

  -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse  
  *)response{
[webData setLength: 0];
  }

  -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
   [webData appendData:data];
  }

  -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
  NSLog(@"Connection Error.");
  }

  -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
  NSLog(@"Bitti. Alinan Veri Byte: %d", [webData length]);
  NSString *xmlString = [[NSString alloc] initWithBytes: [webData mutableBytes] length: 
  [webData length] encoding:NSUTF8StringEncoding];
  NSLog(@"%@", xmlString);
  if (xmlParser) {
    xmlParser = nil;
}

xmlParser = [[NSXMLParser alloc] initWithData:webData];
[xmlParser setDelegate:self];
[xmlParser shouldResolveExternalEntities];
[xmlParser parse];


  }

  int numberOfCount = 0;


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


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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}

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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}

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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}


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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}





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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}



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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}


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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}


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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}

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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}

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



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}
if ( [elementName isEqualToString:@"KB95Tutar"] ) {



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}
if ( [elementName isEqualToString:@"MotorinTutar"] ) {



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}
if ( [elementName isEqualToString:@"LpgTutar"] ) {



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}
  if ( [elementName isEqualToString:@"TutarToplam"] ) {



    if (!retornoSOAP) {
        retornoSOAP = [[NSMutableString alloc] init];
    }
    teveRetorno = YES;

}

  }

  - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
 if (teveRetorno) {
    [retornoSOAP appendString:string];
 }
  }

在标签上显示网络服务字段

  - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{



if ( [elementName isEqualToString:@"BranchName"] ) {
   BranchName.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;



}

  if ( [elementName isEqualToString:@"BranchID"] ) {
    BranchID.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}


 if ( [elementName isEqualToString:@"KB95Miktar"] ) {
    K95MiktarLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}

 if ( [elementName isEqualToString:@"MotorinMiktar"] ) {
    MotorinMiktarLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}
if ( [elementName isEqualToString:@"LpgMiktar"] ) {
    LPGMiktarLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}
if ( [elementName isEqualToString:@"MiktarToplam"] ) {
    ToplamMiktarLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}
if ( [elementName isEqualToString:@"KB95Tutar"] ) {
    K95TutarLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}
if ( [elementName isEqualToString:@"MotorinTutar"] ) {
    MotorinTutarLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}
if ( [elementName isEqualToString:@"LpgTutar"] ) {
    LPGTutarLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}
if ( [elementName isEqualToString:@"TutarToplam"] ) {
    ToplamTutarLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}

if ( [elementName isEqualToString:@"KB95Adet"] ) {
    K95AdetLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}
if ( [elementName isEqualToString:@"MotorinAdet"] ) {
    MotorinAdetLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}

if ( [elementName isEqualToString:@"LpgAdet"] ) {
    LPGAdetLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}

if ( [elementName isEqualToString:@"AdetToplam"] ) {
    ToplamAdetLabel.text = retornoSOAP;

    retornoSOAP = nil;
    teveRetorno = NO;

}

 }



  - (void)selectAnElection:(NSNumber *)selectedIndex element:(id)element {
  self.selectedIndex = [selectedIndex intValue];

//may have originated from textField or barButtonItem, use an IBOutlet instead of element
 self.ElectionsTextField.text = [self.elections objectAtIndex:self.selectedIndex];

}

  - (void)selectProducts:(NSNumber *)selectedIndex element:(id)element {
self.selectedIndex = [selectedIndex intValue];

//may have originated from textField or barButtonItem, use an IBOutlet instead of element
self.ProductsTextfield.text = [self.products objectAtIndex:self.selectedIndex];
}


  - (void)selectCount:(NSNumber *)selectedIndex element:(id)element {
self.selectedIndex = [selectedIndex intValue];

//may have originated from textField or barButtonItem, use an IBOutlet instead of element
self.CountTextfield.text = [self.topnumber objectAtIndex:self.selectedIndex];
  }

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情。

[self createDynamicView];

-(void)createDynamicView{

    CGFloat yOffset = 0;

    for (int i=0;i<5;i++) {  //how many views you want to add in scrollview
        yOffset += 5;
        UILabel* lblHeaderTitle = [[UILabel alloc] initWithFrame:CGRectMake(8, yOffset, 310, 21)];
        [lblHeaderTitle setTextAlignment:UITextAlignmentLeft];
        [lblHeaderTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0f]];
        [lblHeaderTitle setText:[currentDict valueForKey:@"TITLE"]];
        [lblHeaderTitle setTextColor:[UIColor blackColor]];


        [scrlvDynamicContent addSubview:lblHeaderTitle];
        [lblHeaderTitle release];
        //INCREMNET in yOffset 
        yOffset += 25;

    [scrlvDynamicContent setContentSize:CGSizeMake(320, yOffset)];

}