2013-06-26 44 views
2

我已經使用自定義單元格實現了tableview。單元格上有圖像視圖,並且我添加了手勢識別器。但是當我點擊imageview來觸發某個事件時,它會使用數字作爲我滾動表格視圖的方法。假設我滾動tableview 5次然後它imageview也觸發該方法5次。在tableview滾動過程中將事件添加到imageview

這是代碼:

{ 
    if (cell == nil) 
    { 
     cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.frame = CGRectMake(0.0, 0.0, 320.0, 200.0); 
    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.backgroundColor=[UIColor redColor]; 
    [cell.image1 setTag:indexPath.row*2]; 
    [cell.image2 setTag:indexPath.row*2+1]; 

    NSString *pathimage1 =[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"image"]; 
    NSString *filePath1 = [NSString stringWithFormat:@"%@",pathimage1]; 
    NSString *pathimage2=[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"image"]; 
    NSString *filePath2 = [NSString stringWithFormat:@"%@",pathimage2]; 

    UITapGestureRecognizer *tap1 = 
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefirstTap:)]; 
    tap1.delegate=self; 
    tap1.numberOfTapsRequired=1; 
    [cell.image1 addGestureRecognizer:tap1]; 
    [tap1 release]; 
    UITapGestureRecognizer *tap2 = 
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesecondTap:)]; 
    tap2.delegate=self; 
    tap2.numberOfTapsRequired=1; 
    [cell.image2 addGestureRecognizer:tap2]; 
    [tap2 release]; 
    [cell.image1 setImageWithURL:[NSURL URLWithString:filePath1]placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
    [cell.image2 setImageWithURL:[NSURL URLWithString:filePath2]placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

} 
return cell; 
+0

一次通過(細胞==零)阻止 – iSpark

+0

@murali我檢查已經寫我們的代碼檢查。但是通過將代碼放在塊中,它會一次又一次地顯示相同的圖像。 –

+0

請將您的代碼放入您的問題中,而不是鏈接到pastebin。這次我已經爲你修好了。 –

回答

3

這是因爲你在cell == nil條件外添加手勢識別。 TableView每次滾動時都會調用cellForRowAtIndexPath:委託方法。所以你可以通過在cell == nil條件內添加手勢識別器來避免這種情況。

+0

感謝@manujmv它的工作正常。:) –

1

試試這個:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"CustomTableViewCell"; 
    CustomTableViewCell *cell = (CustomTableViewCell *)[tblNameList dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
     cell.showsReorderControl = NO; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.backgroundColor=[UIColor clearColor]; 

     UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefirstTap:)]; 
     tap1.delegate=self; 
     tap1.numberOfTapsRequired=1; 
     [cell.image1 addGestureRecognizer:tap1]; 
     [tap1 release]; 

     UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesecondTap:)]; 
     tap2.delegate=self; 
     tap2.numberOfTapsRequired=1; 
     [cell.image2 addGestureRecognizer:tap2]; 
     [tap2 release]; 
    } 

    [cell.image1 setTag:indexPath.row*2]; 
    [cell.image2 setTag:indexPath.row*2+1]; 

    NSString *pathimage1 = [[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"image"]; 
    NSString *filePath1 = [NSString stringWithFormat:@"%@",pathimage1]; 
    NSString *pathimage2 = [[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"image"]; 
    NSString *filePath2 = [NSString stringWithFormat:@"%@",pathimage2]; 

    [cell.image1 setImageWithURL:[NSURL URLWithString:filePath1]placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
    [cell.image2 setImageWithURL:[NSURL URLWithString:filePath2]placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    return cell; 
} 
+0

我也試過,但它通過加載筆尖使得滾動很難。 –

+0

看到我編輯的答案... – DharaParekh

1

//嘗試這個..

//在.H

UITapGestureRecognizer *tap1[100]; 
UITapGestureRecognizer *tap2[100]; 

//在.M

{ 
    if (cell == nil) 
    { 
     cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.frame = CGRectMake(0.0, 0.0, 320.0, 200.0); 
    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.backgroundColor=[UIColor redColor]; 
    [cell.image1 setTag:indexPath.row*2]; 
    [cell.image2 setTag:indexPath.row*2+1]; 

    if(!tap1[indexPath.row]) 
{ 
    tap1[indexPath.row] = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefirstTap:)]autorelease]; 
    tap1.delegate=self; 
    tap1.numberOfTapsRequired=1; 
    [cell.image1 addGestureRecognizer:tap1]; 
} 
    if(!tap2[indexPath.row]) 
{ 
    tap2[indexPath.row] = 
    [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesecondTap:)]autorelease]; 
    tap2.delegate=self; 
    tap2.numberOfTapsRequired=1; 
    [cell.image2 addGestureRecognizer:tap2]; 
} 
    [cell.image1 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"image"]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    [cell.image2 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"image"]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

} 
return cell; 
+0

其他明智的只在自定義單元格中創建UITapGestureRecognizer並在cellforrowatindexpath中給出addtarget。這將是永久的解決方案。 –

1

//嘗試this..this代碼將有益的你根據內存管理..

//定製單元中..

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) 
    { 
     image1 = [[UIImageView alloc]init]; 
     [self addSubview: image1]; 

     tap1 = [[UITapGestureRecognizer alloc] init]; 
     tap1.numberOfTapsRequired=1; 
     [image1 addGestureRecognizer:tap1]; 
    } 
} 

//在.M

{ 
    if (cell == nil) 
    { 
     cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.frame = CGRectMake(0.0, 0.0, 320.0, 200.0); 
    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.backgroundColor=[UIColor redColor]; 
    [cell.image1 setTag:indexPath.row*2]; 
    [cell.image2 setTag:indexPath.row*2+1]; 

    [cell.image1 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"image"]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    [cell.image2 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"image"]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

     [cell.tap1 addTarget:self action:@selector(handlefirstTap:)]; 
     [cell.tap2 addTarget:self action:@selector(handlesecondTap:)]; 
} 
return cell; 
相關問題