开发者

Logic for adding images and scroll icon to the table view

开发者 https://www.devze.com 2023-03-11 05:49 出处:网络
I have a table view, and I am adding four UIImageViews to a row, so in total I have five rows and twenty image views. I am inserting images one by one into the cell. The second row will only start to

I have a table view, and I am adding four UIImageViews to a row, so in total I have five rows and twenty image views. I am inserting images one by one into the cell. The second row will only start to fill once all four im开发者_运维百科age views are filled.

I need some logic for this. I mean, how can I check which place is next to be filled in the table view and at which event? How can I add images one by one to the UIImageViews? Also, initially only two rows will be shown. After filling these two rows, when images begin to enter into the third row, I need to show an icon adjacent to the last row that is on the screen, to tell the user that there is more data below. Also, is there any event to detect which row is currently the last row on screen?

Also is there any way to individually select the images from a row?


Run this code:-

.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 file code:-

@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];

}


You should keep all of the logic (and data) in your table view controller (UITableViewController subclass). I suggest you have a custom UITableViewCell subclass that can hold 4 images (the cell itself should be responsible for the layout), and the table view controller is responsible for setting it up. In -tableView:cellForRowAtIndexPath: you can look at your collection of images, then just use modular arithmetic to figure out which images should be in the cell for that particular index path.

As for individually selecting images, perhaps you could have your cells know their index path (set as a custom property in -tableView:cellForRowAtIndexPath:), then each have a delegate method telling your controller that a certain image was pressed (for example, -cellAtIndexPath:didSelectImageAtIndex:).


try this, As there are fix number of images that you want to show in your row i. e. 4 So what you have to do is:- in .h file

int widthview,widthview1;

in .m file in viewdidload

   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;

}

This code is for 2 rows do the same thing for next three rows. Adjust the coordinates according to your requirement. hope this will save your time.


answer for Also is there any way to individually select the images from a row? : Give image to button and add that button inside row!!

0

精彩评论

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