如何在UITableView中添加更多负载

时间:2013-10-24 13:37:23

标签: ios objective-c uitableview

你能帮助我加载更多UITableView吗?我使用加载更多方法,但所有记录返回而不是25.代码中出了什么问题?请帮助:S谢谢你的回复

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

  if (self) {
  // Custom initialization

 }

  return self;
}




- (void)viewDidLoad{ 
itemArray = [[NSMutableArray alloc] init];
[self ServiseBaglan];}

- (void)ServiseBaglan{
   NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                         "<SOAP-ENV:Envelope \n"
                         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
                         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
                         "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\n"
                         "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
                         "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
                         "<SOAP-ENV:Body> \n"
                         "<OnemliYer xmlns=\"http://tempuri.org/\">\n"
                         "<station>%@</station>\n"
                         "<dilId>%@</dilId>\n"
                         "</OnemliYer> \n"
                         "</SOAP-ENV:Body> \n"
                         "</SOAP-ENV:Envelope>\n", izmir , @"1"];

 NSURL *url = [NSURL URLWithString:@"http://www.manavgatportal.com/Service1.svc"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[theRequest addValue: @"http://tempuri.org/IService1/OnemliYer" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

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

if(theConnection) {
    //  webData = [[NSMutableData data] retain];
    webData = [[NSMutableData data]init];
}
else {
    NSLog(@"theConnection is NULL");
}

[self.tableView reloadData];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath            
*)indexPath{
return 40;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 return 1;
}

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

 if ([itemArray count] >= 25) {
 count= [itemArray count] + 1;
 NSLog(@": %d", count);

 }else
 {
  count=[itemArray count];
     NSLog(@": %d", count);
 }


 return count;


  }



  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    
  (NSIndexPath *)indexPath
   {
   static NSString *CellIdentifier = @"Cell";

   static NSString *CellIdentifier2 = @"Cell2";
static NSInteger TitleTag = 2;
static NSInteger AddresseTag = 1;
    UITableViewCell *cell;
   if (indexPath.row != [itemArray count] ) {
   cell= (UITableViewCell *) [tableView   
   dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
    reuseIdentifier:CellIdentifier];

    }

    NSDictionary *Item = [itemArray objectAtIndex:indexPath.row];
    UIFont *font = [UIFont fontWithName:@"Arial" size:14];
    cell.textLabel.font = font;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    cell.detailTextLabel.numberOfLines = 2;
    cell.textLabel.text = [Item objectForKey:@"a:Ad"];
    cell.detailTextLabel.text = [Item objectForKey:@"a:Ilce"];

    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];

}
else {
    if(indexPath.row == count-1)
    {

        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
        }

        UILabel *loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,320,30)];
        loadMore.textColor = [UIColor blackColor];
        loadMore.highlightedTextColor = [UIColor darkGrayColor];
        loadMore.backgroundColor = [UIColor clearColor];
        loadMore.font=[UIFont fontWithName:@"Verdana" size:14];
        loadMore.textAlignment=UITextAlignmentCenter;
        loadMore.font=[UIFont boldSystemFontOfSize:14];
        loadMore.text=@"load More...";
        [cell addSubview:loadMore];
       }
     }



      return cell;
    }


    - (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
     }

1 个答案:

答案 0 :(得分:0)

我认为在count中重新定义numberOfRowsInSection变量并不是一个好设计。

相反,您应该在实际更改itemsArray的位置执行此操作。一个好的地方是在视图初始化期间,并在按下“再加载25”之后。

此外,您可以考虑完全删除变量并改为使用itemArray.count+1