我有一个表视图,我在一行中添加了四个UIImageView
,所以我总共有五行和二十个图像视图。我将图像逐个插入单元格。只有在填充了所有四个图像视图后,第二行才会开始填充。
我需要一些逻辑。我的意思是,如何在表格视图和哪个事件中检查下一个要填写的位置?如何将图像逐个添加到UIImageView
s?此外,最初只会显示两行。填充这两行后,当图像开始进入第三行时,我需要在屏幕上的最后一行旁边显示一个图标,告诉用户下面有更多数据。此外,是否有任何事件要检测哪一行当前是屏幕上的最后一行?
还有什么方法可以单独从一行中选择图像吗?
答案 0 :(得分:1)
您应该将所有逻辑(和数据)保存在表视图控制器(UITableViewController
子类中)中。我建议你有一个自定义UITableViewCell
子类,它可以容纳4个图像(单元格本身应该负责布局),并且表视图控制器负责设置它。在-tableView:cellForRowAtIndexPath:
中,您可以查看自己的图像集,然后使用modular arithmetic来确定特定索引路径中单元格中应包含哪些图像。
至于单独选择图像,也许你可以让你的单元知道它们的索引路径(在-tableView:cellForRowAtIndexPath:
中设置为自定义属性),然后每个都有一个委托方法告诉你的控制器按下某个图像(对于例如,-cellAtIndexPath:didSelectImageAtIndex:
)。
答案 1 :(得分:1)
试试这个, 因为你想要在你的行中显示固定数量的图像i。即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行,对下三行执行相同的操作。 根据您的要求调整坐标。 希望这会节省你的时间。
答案 2 :(得分:1)
运行此代码: -
.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.
// e.g. 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];
}
答案 3 :(得分:0)
回答也有什么方法可以单独从一行中选择图像? : 将图像提供给按钮并在行内添加该按钮!!