1

大家好,我有一个带有滚动视图的自定义表格视图单元格。问题是所有数据都一次加载到表格视图单元格中。我想要的是仅为 tableView 中的可见单元格加载数据。我使用 [indexPath forvisiblerows ]; 这是徒劳的..cud你们帮我下面是代码。

-(IPadTableViewCell *)getNewCell
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"IpadTableViewCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
{
    if ([currentObject isKindOfClass:[IPadTableViewCell class]])
    {
        cell= (IPadTableViewCell  *)currentObject;
        return cell;   
    }
}
return nil;
 }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   //  static NSString *CellIdentifier = @"IpadTableViewCell";
   //cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
id1=[NSString stringWithFormat:@"%i",indexPath.row];
   cell=[dicAlbumCells objectForKey:id1];
if (cell == nil) {
    cell=[self getNewCell]; 
}
cell.tag=indexPath.row;
return cell;
}

  -(void)loadImagesForOnscreenRows
  {
 NSArray *visiblePaths = [tblGallery indexPathsForVisibleRows];

for (NSIndexPath *indexPath in visiblePaths) {
    if (imageArray > 0) {

       [self imageButtonClicked:nil];
    }
   }
  }

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
 {        
    [self loadImagesForOnscreenRows];
}
 }
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self loadImagesForOnscreenRows];
}
 -(void)imageButtonClicked:(id)sender
{

CGFloat prevButtonXPosition = 18, pageButtonXPosition, pageButtonWidth = 144, pageButtonHeight = 143;

for (NSInteger i =0; i<9; i++) {
    pageButtonXPosition = prevButtonXPosition ;
    prevButtonXPosition = pageButtonXPosition+pageButtonWidth+20;
    UIButton *pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [pageButton setContentMode:UIViewContentModeScaleAspectFit];
    pageButton.layer.borderColor=[[UIColor whiteColor]CGColor];
    pageButton.layer.borderWidth = 3;
    pageButton.layer.cornerRadius = 0;
    [pageButton addTarget:self action:@selector(onClickImage:) forControlEvents:UIControlEventTouchUpInside];
    [pageButton setFrame:CGRectMake(pageButtonXPosition,0, pageButtonWidth, pageButtonHeight)];
    [pageButton setBackgroundImage:[UIImage imageNamed:[imageArray objectAtIndex:i]] forState:UIControlStateNormal];
    [pageButton setTag:i];
    [cell.scrollView addSubview:pageButton];
  }

UIButton *add=[UIButton buttonWithType:UIButtonTypeCustom];
add.layer.borderColor=[[UIColor whiteColor]CGColor];
add.layer.borderWidth = 3;
add.layer.cornerRadius = 0;
[add addTarget:self action:@selector(onClickAdd:) forControlEvents:UIControlEventTouchUpInside];
[add setFrame:CGRectMake(prevButtonXPosition, 0, 144, 143)];
[add setBackgroundImage:[UIImage imageNamed:@"grey.png"] forState:UIControlStateNormal];
[add setTag:0];
[cell.scrollView addSubview:add];

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(prevButtonXPosition+35,30, 76,76)];
[iv setImage:[UIImage imageNamed:@"last.png"]];
[cell.scrollView addSubview:iv];

[cell.scrollView setContentSize:CGSizeMake(prevButtonXPosition +165, 40)];
 cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:cell.scrollView];
 }
4

0 回答 0