如何像UITableView一样重新排序UIScrollView行

时间:2011-09-14 06:30:24

标签: iphone objective-c ios uiscrollview

我正在创建一个应用程序,我需要重新排序类似于uitableview的uiscrollview行,所以我想知道是否可以做,如果是,那么请帮助我...

2 个答案:

答案 0 :(得分:0)

UITableView可以轻松对行进行重新排序,如果您需要获得相同的行为,请在UIScrollView中自行完成所有操作......

答案 1 :(得分:0)

如果你想做类似的事情那么我假设你没有考虑使用自定义UITableViewCells的表视图?如果您要创建类似库的内容,请尝试使用一些自定义视图创建一个UITableViewCell,该视图包含您需要显示的图像和所有其他数据:

.h文件

#import <UIKit/UIKit.h>
#import "ClipView.h"

@interface ClipViewCell : UITableViewCell {

}

@property (nonatomic, retain) NSMutableArray *clipViews;

- (void)setVisibleClipViewsQuantity:(NSUInteger)quantity;

@end

.m文件

#import "ClipViewCell.h"


@implementation ClipViewCell

@synthesize clipViews;

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier {

  self = [super initWithStyle:style
              reuseIdentifier:reuseIdentifier];

  if (self) {
    self.clipViews = [NSMutableArray array];

    CGFloat xMargin = 5.0f;
    for (int i = 0; i < 3; i++) {
      ClipView *clipView = [[[ClipView alloc] initWithFrame:CGRectMake(xMargin,
                                                                       5.0f,
                                                                       0.0f,
                                                                       0.0f)] autorelease];
      [clipViews addObject:clipView];
      xMargin = clipView.frame.origin.x + clipView.frame.size.width + 5.0f;
      [self addSubview:clipView];
    }
  }
  return self;
}

- (void)setVisibleClipViewsQuantity:(NSUInteger)quantity {
  for (int i = quantity; i < 3; i++) {
    ClipView *clipView = [clipViews objectAtIndex:i];
    clipView.hidden = YES;
  }

  for (int i = 0; i < quantity; i++) {
    ClipView *clipView = [clipViews objectAtIndex:i];
    clipView.hidden = NO;
  }
}

- (void)setSelected:(BOOL)selected
           animated:(BOOL)animated {
  return;
}

- (void)dealloc {
  self.clipViews = nil;
  [super dealloc];
}

@end

其中“ClipView”是您自定义的UIView,其上显示数据。

然后在您的UITableView视图控制器中使用表视图数据源方法,如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  NSInteger number = clips.count % 3 > 0 ? clips.count / 3 + 1 : clips.count / 3;
  return number;
}

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

  ClipViewCell *cell = nil;
  int clipViewsQuantity = 0;

    NSString *reuseIdentifier = @"clipCell";
    if (indexPath.row == [tableView numberOfRowsInSection:0]-1) {
     clipViewsQuantity = myLingoClips.count % 3 > 0 ? myLingoClips.count % 3 : 3;
    }
    else {
      clipViewsQuantity = 3;
    }


  cell = (ClipViewCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
  if (cell == nil) {
    cell = [[ClipViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
  }

  [cell setVisibleClipViewsQuantity:clipViewsQuantity];
  int currentElement = indexPath.row * 3;

  for (int i = 0; i < clipViewsQuantity; i++) {
    ClipView *clipView = [cell.clipViews objectAtIndex:i];
    // do something with the clipView here, like set another image
    currentElement++;
  }

  return cell;
}

使用此功能,您可以轻松调整数据模型,以便使用标准UITableView功能进行重新排序。

相关问题