我有像arraypartno
这样的数组,它包含80
个值,必须显示20-20 values
中的tableview cell
和next 20 values
上的click
下一个按钮就像分页一样
但是,当我点击下一个按钮时,它会添加值并将单元格增加为40
,然后60
值等等。但是我首先要20 values
20 values
tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor=[UIColor clearColor];
UILabel *lblplate1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200, 30)];
lblplate1.text = [arraypartno objectAtIndex:indexPath.row];
lblplate1.textAlignment=UITextAlignmentLeft;
lblplate1.textColor = [UIColor colorWithRed:2.0/255.0 green:143.0/255.0 blue:213.0/255.0 alpha:1];
lblplate1.backgroundColor =[UIColor clearColor];
lblplate1.font = [UIFont fontWithName:@"Helvetica" size:14];
[cell.contentView addSubview:lblplate1];
[lblplate1 release];
lblplate1 = nil;
return cell;
-(void)next:(id)sender
{
lowvalue= lowvalue+20;
NSString *str = [[NSString alloc]initWithFormat:@"http://demo.com/New/web_serv/wspartsearch.php?search=%@&start=%d&count=20",searchText,lowvalue];
}
,就像分页....
请指导我解决这个问题。谢谢你
表格视图单元格的代码: -
-(void)previous:(id)sender
{
lowvalue= lowvalue-20;
NSString *str = [[NSString alloc]initWithFormat:@"http://demo.com/New/web_serv/wspartsearch.php?search=%@&start=%d&count=20",searchText,lowvalue];
}
}
lowvalue从0初始化,单击下一个按钮时会增加
{{1}}
单击上一个按钮时会减少
{{1}}
我一次只想要20个值。
答案 0 :(得分:1)
类似的东西,
初始化totalShowCount = 20;
(totalShowCount < [arraypartno count])
1)在numberOfRowsInSection
数据源方法return totalShowCount-1;
2)在下一个按钮操作中,使用下一个20个值更新totalShowCount
,例如totalShowCount+=20
;
3)[tableview reloadData];
您可能需要在下一个按钮if(totalShowCount<[arraypartno count])
上使用某些条件,然后才执行第2步和第3步。
答案 1 :(得分:1)
默认numberOfItemsToDisplay = 20;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (numberOfItemsToDisplay >= [self.aryAlerts count])
{
numberOfItemsToDisplay = [self.aryAlerts count];
return 1;
}
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return numberOfItemsToDisplay;
}
else
{
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *CellIdentifier = @"YourCustomCell";
YourCustomCell *objYourCustomCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (objYourCustomCell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"YourCustomCell" owner:self options:nil];
for(id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[YourCustomCell class]])
{
objYourCustomCell = (AlertsCustomCell *) currentObject;
break;
}
}
}
objYourCustomCell.lbl.text = [[self.aryAlerts objectAtIndex:indexPath.row]valueForKey:@"vName"];
return objYourCustomCell;
}
else
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.contentView addSubview:[self loadMoreViewForTable:self.view]];
}
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tblAlertsList deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 1)
{
numberOfItemsToDisplay = [self loadMore:numberOfItemsToDisplay arrayTemp:self.aryAlerts tblView:self.tblAlertsList];
[self.tblAlertsList endUpdates];
}else
{
// action if it is not LoadMore
}
}
+ (NSInteger)loadMore : (NSInteger)numberOfItemsToDisplay arrayTemp:(NSMutableArray*)aryItems tblView:(UITableView*)tblList
{
int count =0;
NSUInteger i, totalNumberOfItems = [aryItems count];
NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd);
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++)
{
count++;
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
numberOfItemsToDisplay = newNumberOfItemsToDisplay;
[tblList beginUpdates];
[tblList insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
if (numberOfItemsToDisplay == totalNumberOfItems) {
[tblList deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];
}
NSIndexPath *scrollPointIndexPath;
if (newNumberOfItemsToDisplay < totalNumberOfItems)
{
scrollPointIndexPath = [NSIndexPath indexPathForRow:numberOfItemsToDisplay-kNumberOfItemsToAdd inSection:0];
}
else
{
scrollPointIndexPath = [NSIndexPath indexPathForRow:i-count inSection:0];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100000000), dispatch_get_main_queue(), ^(void){
[tblList scrollToRowAtIndexPath:scrollPointIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
});
return numberOfItemsToDisplay;
}
答案 2 :(得分:1)
我假设您已经预定义了80个对象的初始化数组,为数据源创建了第二个NSMutableArray
我将其命名为dataElements
,这将在tableView
中显示数据。现在每个{ {1}}(或者,如果您在同一视图中,则在ViewDidLoad
中进行此更新)进行循环,并从“nextMethod
”dataElements
复制20个值
例如
1.Make arraypartno
2.make int counter=0;
。我将其命名为NsMutableArray
dataElements
如果你仍然有困惑,请问它。
答案 3 :(得分:0)
首先,您需要删除NSArray
中已有的对象,然后再添加其中的20个对象。
尝试:
[myArray removeAllObjects];
然后在数组中添加下20个对象。
然后重新加载你的tableview。