This question already has an answer here:
这个问题已经有了答案:
- How to implement “Load 25 More” in UITableViewController 4 answers
- 如何在UITableViewController 4中实现“加载25更多”?
Can you help me about load more UITableView
? I use load more methods, but all records returns instead of 25.What is wrong in code? please help :S Thanks for replying
你能不能帮我看一下load more UITableView?我使用了加载更多的方法,但是所有的记录返回而不是25。代码有什么问题?请帮忙:谢谢你的回复。
- (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 个解决方案
#1
0
I do not think it is good design to redefine your count
variable in numberOfRowsInSection
.
我不认为在numberOfRowsInSection中重新定义count变量是很好的设计。
Rather, you should do this where you actually change the itemsArray
. A good place is during the initialisation of the view, and after pressing "load 25 more".
相反,您应该在实际更改itemsArray的地方这样做。一个好的地方是在视图的初始化过程中,在按下“加载25更多”之后。
Also, you could consider eliminating the variable altogether and using itemArray.count+1
instead.
此外,您可以考虑完全消除变量并使用itemArray。数+ 1。
#1
0
I do not think it is good design to redefine your count
variable in numberOfRowsInSection
.
我不认为在numberOfRowsInSection中重新定义count变量是很好的设计。
Rather, you should do this where you actually change the itemsArray
. A good place is during the initialisation of the view, and after pressing "load 25 more".
相反,您应该在实际更改itemsArray的地方这样做。一个好的地方是在视图的初始化过程中,在按下“加载25更多”之后。
Also, you could consider eliminating the variable altogether and using itemArray.count+1
instead.
此外,您可以考虑完全消除变量并使用itemArray。数+ 1。