开发者

How to use the uicontrolstate function in the table view cell image?

开发者 https://www.devze.com 2023-02-09 00:59 出处:网络
Am using the uiimageview in the CellForRowAtIndext as like below int k=5; for(int i=1; i<=3;i++) { btn_img=[[UIImageView alloc]initWithFrame:CGRectMake(k,50,90, 40)];

Am using the uiimageview in the CellForRowAtIndext as like below

int k=5;
for(int i=1; i<=3;i++) {

    btn_img=[[UIImageView alloc]initWithFrame:CGRectMake(k,50,90, 40)];
    btn_img.image = [UIImage imageNamed:[image_array1 objectAtIndex:indexPath.row]];
    btn_img.tag=i;
    [cell.contentView addSubview:btn_img];
    k=k+95;
    [btn_img release];

}

through this I can able to display images 3 in a row. but each image are differ. If particular image is clicked. How can I identify that particular image id?

for(int i=0; i<=2;i++) { 
if (m<[image_array1 count]) { 
    btn_img=[[UIButton alloc]initWithFrame:CGRectMake(k,5,90, 50)]; 
    UIImage *buttonImage= [UIImage imageNamed:[image_array1 objectAtIndex:m]]; 
    [btn_img setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
    btn_img.tag=i; 
    UITapGestureRecognizer *tap_gr = [[UITapGestureRecognizer alloc]    initWithTarget:self action:@selecto开发者_运维知识库r(handleTap:)]; 
    [tap_gr setNumberOfTapsRequired:1]; 
    [btn_img addGestureRecognizer:tap_gr]; 
    [tap_gr release]; 
    [cell.contentView addSubview:btn_img]; 
    k=k+95; 
    [btn_img release]; 
    m++; 
}
}

I confused how to write in the didselectrowatindexpath.

Thanks!

Senthilkumar


You can use UITapGestureRecognizer. Add the tap gesture recognizer to btn_img as follows.

UITapGestureRecognizer *tap_gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[tap_gr setNumberOfTapsRequired:1];
[btn_img addGestureRecognizer:tap_gr];
[tap_gr release];

In your handle tap method, you can access the tag of the image view that is tapped, and do the necessary action.

- (void)handleTap:(id)sender {

    NSLog(@"Tapped ImageView tag : %d", [sender tag]);
}


You can use UICustomButtons for that purpose.Set the background image of buttons,set the id of buttons while assigning image to a particular button.So when a particular button is tapped,the id of that button can be identified.Write a method to perform the required operations.

Cheers

0

精彩评论

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