2

我有类似的数组arraypartno,它包含80值,必须显示下一个按钮的20-20 values输入tableview cellnext 20 values打开click,如分页。
但是当我点击下一个按钮时,它会添加值并增加单元格40,然后是60值等等。但我想20 values20 valuestableview单元格上再下一个,比如分页....

请指导我解决这个问题。感谢您

这是表格视图单元格的代码:-

 - (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;

}

低值从0初始化,点击next按钮时增加

 -(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];

}

我一次只想要 20 个值。

4

4 回答 4

1

就像是,

初始化totalShowCount = 20; (totalShowCount < [arraypartno count])

1) 在numberOfRowsInSection数据源方法中return totalShowCount-1;

2) 在下一个按钮操作中,totalShowCount使用下一个 20 值更新,例如totalShowCount+=20

3)[tableview reloadData];

您可能需要在下一步按钮上设置一些条件,if(totalShowCount<[arraypartno count])然后只执行第 2 步和第 3 步。

于 2013-02-22T06:45:58.153 回答
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;
    }
于 2013-02-22T06:50:17.447 回答
1

我假设您已经预定义了 80 个对象的初始化数组,NSMutableArray为数据源创建了第二个,我将其命名为dataElements将在tableView.Now 中显示数据。现在每次ViewDidLoad(或者如果您在同一视图中,则在您的 中进行此更新nextMethod)进行循环并dataElements从您的“ ”中复制 20 个值,arraypartno例如 1.Make int counter=0; 2.make NsMutableArray。我将其命名为dataElements

-(IBAction)nextButtonPressed:(id)sender{

            if(counter < 20){
                for(int i=0 ; i<20 ;i++ ){
                    [dataElements addObject:[arraypartno objectAtIndex:i]];
                    counter++;
                }
            }else if(counter<40){
                for(int i=20 ; i<40 ;i++ ){
                    [dataElements replaceObjectAtIndex:i withObject:[arraypartno objectAtIndex:i]];
                    counter++;
                }
            }else if(counter<60){
                for(int i=40 ; i<60 ;i++ ){
                    [dataElements replaceObjectAtIndex:i withObject:[arraypartno objectAtIndex:i]];
                    counter++;
                }
            }else if(counter<80){
                for(int i=60 ; i<80 ;i++ ){
                    [dataElements replaceObjectAtIndex:i withObject:[arraypartno objectAtIndex:i]];
                    counter++;
                }
            }
            [self.tableView reloadData];
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataElements count];



    //OR
    // return 20;
    }

如果您仍然有困惑,请询问。

于 2013-02-22T07:55:21.293 回答
0

首先,您需要先删除已经存在的对象,NSArray然后再添加接下来的 20 个对象。

尝试:

[myArray removeAllObjects];

然后在您的数组中添加接下来的 20 个对象。

然后重新加载您的表格视图。

于 2013-02-22T06:36:20.997 回答