开发者

how to reorder UIScrollView row like UITableView

I am creating an app in which i need to reorder the uiscrollview rows similar to uitableview so i want to know is that possible to do if yes th开发者_运维问答en how please help me...


UITableView makes it easy to reorder rows, if you need to get the same behavior, in UIScrollView you need to do it all yourself...


If you want to do something like that then I'm assuming that you didn't consider using a table view with custom UITableViewCells instead? If you are creating something like gallery, try creating a UITableViewCell with a few custom views, which hold images and all additional data you need to display:

.h file

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

@interface ClipViewCell : UITableViewCell {

}

@property (nonatomic, retain) NSMutableArray *clipViews;

- (void)setVisibleClipViewsQuantity:(NSUInteger)quantity;

@end

.m file

#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

Where "ClipView" is your custom UIView with displayed data on it.

Then in your UITableView view controller use table view data source methods like:

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

Using this one you can easily adjust your data model for reordering with standard UITableView functionality.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜