用于添加图像和滚动图标到表格视图的逻辑

我有一个表格视图,并且我将四个UIImageView添加到一行,所以总共有五行和二十个图像视图。 我将图像逐一插入到单元格中。 一旦所有四个图像视图被填充,第二行将开始填充。

我需要一些逻辑。 我的意思是,我怎样才能检查哪个地方是下一个被填写在表视图和在哪个事件? 如何将图像逐个添加到UIImageView ? 另外,最初只显示两行。 填充这两行后,当图像开始进入第三行时,我需要在屏幕上的最后一行旁边显示一个图标,告诉用户下面有更多的数据。 另外,是否有任何事件来检测哪一行是当前屏幕上的最后一行?

还有什么办法可以单独从一行中select图像?

运行这个代码: –

 .h file code #import <UIKit/UIKit.h> @interface helpTableViewController : UIViewController { IBOutlet UITableView *gridTableView; NSMutableArray *imagesArray; int widthview,widthview1; } @property(nonatomic,retain)NSMutableArray *imagesArray; @property(nonatomic,retain)UITableView *gridTableView; -(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath; -(void)crossViewButtonClicked:(UIButton *)sender; @end 

.m文件代码: –

 @synthesize gridTableView; @synthesize imagesArray; // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *tempArray=[[NSMutableArray alloc]init]; self.imagesArray=tempArray; [tempArray release]; [self.imagesArray addObject:[UIImage imageNamed:@"pic1.png"]]; [self.imagesArray addObject:[UIImage imageNamed:@"pic2.png"]]; [self.imagesArray addObject:[UIImage imageNamed:@"pic3.png"]]; [self.imagesArray addObject:[UIImage imageNamed:@"pic4.png"]]; [self.imagesArray addObject:[UIImage imageNamed:@"pic5.png"]]; [self.imagesArray addObject:[UIImage imageNamed:@"pic6.png"]]; [self.imagesArray addObject:[UIImage imageNamed:@"pic1.png"]]; [self.imagesArray addObject:[UIImage imageNamed:@"pic2.png"]]; [self.imagesArray addObject:[UIImage imageNamed:@"pic3.png"]]; widthview=0; widthview1=0; } - (void)viewDidUnload { self.gridTableView=nil; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (void)dealloc { [imagesArray release]; [gridTableView release]; [super dealloc]; } #pragma mark - #pragma mark TableView Methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell=nil; if (cell == nil) { cell =[self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath]; } return cell; } -(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; if (indexPath.row==0) { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)]; scrollView.contentSize = CGSizeMake(320, 180); scrollView.backgroundColor=[UIColor clearColor]; scrollView.showsHorizontalScrollIndicator = NO; [cell.contentView addSubview:scrollView]; for (int i=0; i<4;i++) { UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)]; profileView.backgroundColor=[UIColor whiteColor]; UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)]; [profileimageView setImage:[self.imagesArray objectAtIndex:i]]; profileimageView.tag=i+90; [profileView addSubview:profileimageView]; [profileimageView release]; [scrollView addSubview:profileView]; widthview=widthview+105; NSLog(@"%d",widthview); } UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(widthview1+10, 12, 95, 155)]; //button.contentMode = UIViewContentModeScaleAspectFill; button.tag=999999; [button setBackgroundColor:[UIColor greenColor]]; [button addTarget:self action:@selector(imageButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [scrollView addSubview:button]; widthview=widthview+105; scrollView.contentSize = CGSizeMake(widthview, 180); } if (indexPath.row==1) { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)]; scrollView.contentSize = CGSizeMake(320, 180); scrollView.backgroundColor=[UIColor clearColor]; scrollView.showsHorizontalScrollIndicator = NO; [cell.contentView addSubview:scrollView]; for (int i=0; i<4;i++) { UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview1+10, 12, 95, 155)]; profileView.backgroundColor=[UIColor whiteColor]; UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)]; // [profileimageView setImage:[UIImage imageNamed:@"gridimage1.png"]]; profileimageView.tag=4+i; [profileimageView setImage:[self.imagesArray objectAtIndex:3+i]]; [profileView addSubview:profileimageView]; [scrollView addSubview:profileView]; [profileimageView release]; widthview1=widthview1+105; NSLog(@"%d",widthview1); } scrollView.contentSize = CGSizeMake(widthview1, 180); }return cell; } // Override to support row selection in the table view. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } #pragma mark - #pragma mark button methods -(void)imageButtonClicked:(UIButton *)sender { [self.imagesArray replaceObjectAtIndex:0 withObject:[UIImage imageNamed:@"abdulprofilepic.png"]]; [self.gridTableView reloadData]; } 

你应该保留所有的逻辑(和数据)在你的表视图控制器( UITableViewController子类)。 我build议你有一个自定义的UITableViewCell子类可以容纳4个图像(单元本身应该负责布局),而表视图控制器负责设置它。 在-tableView:cellForRowAtIndexPath:你可以看看你的图像集合,然后使用模块化algorithm找出哪些图像应该在单元格中的特定索引path。

至于单独select图片,也许你可以让你的单元格知道他们的索引path(在-tableView:cellForRowAtIndexPath:设置为一个自定义属性),然后每个都有一个委托方法告诉你的控制器一个特定的图像被按下(例如, -cellAtIndexPath:didSelectImageAtIndex:

试试这个,因为有你想要显示在你的行,即4固定数量的图像所以你要做的是: – 在.h文件

 int widthview,widthview1; 

在viewdidload的.m文件中

  widthview=0; widthview1=0; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath]; } cell.selectionStyle = UITableViewCellSelectionStyleNone;//to remove blu selection color return cell; } -(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; if (indexPath.row==0) { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)]; scrollView.contentSize = CGSizeMake(320, 180); scrollView.backgroundColor=[UIColor clearColor]; scrollView.showsHorizontalScrollIndicator = NO; [cell.contentView addSubview:scrollView]; for (int i=0; i<4;i++) { UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)]; [profileimageView setImage:[self.imagesArray objectAtIndex:i]]; [profileimageView release]; [scrollView addSubview:profileimageView]; widthview=widthview+105; NSLog(@"%d",widthview); } scrollView.contentSize = CGSizeMake(widthview, 180); } if (indexPath.row==1) { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)]; scrollView.contentSize = CGSizeMake(320, 180); scrollView.backgroundColor=[UIColor clearColor]; scrollView.showsHorizontalScrollIndicator = NO; [cell.contentView addSubview:scrollView]; for (int i=0; i<4;i++) { UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)]; [profileimageView setImage:[self.imagesArray objectAtIndex:4+i]]; [profileimageView release]; [scrollView addSubview:profileimageView]; widthview1=widthview1+105; NSLog(@"%d",widthview1); } scrollView.contentSize = CGSizeMake(widthview1, 180); } return cell; } 

这个代码是为2行做相同的事情为接下来的三行。 根据您的要求调整坐标。 希望这会节省您的时间。

答案也有没有办法单独从行中select图像? :给图片button,并添加该行内的button!