2014-02-17 116 views
0

可能是一個簡單的修復。更改表格視圖單元格的UIImage出現

我有我的表視圖控制器的各個小區,在它的點按手勢識別器內的圖像。這工作正常,記錄適當。

我需要的是圖像從默認狀態改變,然後在「選擇」狀態(綠色)和「取消選擇」狀態(紅色)之間切換。換句話說,這是一個首先是灰色的清單,然後點擊圖像,圖像變爲綠色複選標記,再次點擊,變爲紅色x。

這裏有一個問題:如果這只是didSelectRowAtIndexPath方法中條件語句的問題,但由於我添加了手勢識別器並創建了imageTapped方法,我似乎無法將其翻譯。因此,this等其他有用的線索不適用於我。

一如既往的感謝。你們是最棒的。

下面的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"PlacesCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PlacesCell"]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    //Create ImageView 
    cell.imageView.image = [UIImage imageNamed:@"checkmark.png"]; 

    //Add Gesture Recognizer 
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped)]; 
    tapped.numberOfTapsRequired = 1; 
    [cell.imageView addGestureRecognizer:tapped]; 
    cell.imageView.userInteractionEnabled = YES; 
    [cell addSubview:cell.imageView]; 

    return cell; 

    } 

//Method controlling what happens when cell's UIImage is tapped 
-(void)imageTapped 
{ 

    UITableViewCell *cell; 

    UIImage *imageDefault = [UIImage imageNamed:@"checkmark.png"]; 
    UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"]; 
    UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"]; 

    if (cell.imageView.image == imageDefault) { 
     cell.imageView.image = imageGreen; 
     cell.selected = true; 
     NSLog(@"Selected"); 
    } else { 
     cell.imageView.image = imageRed; 
     cell.selected = false; 
     NSLog(@"Deselected"); 
    } 
} 

回答

4

我已修改了代碼,查它

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"PlacesCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PlacesCell"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 

//Create ImageView 
cell.imageView.image = [UIImage imageNamed:@"checkmark.png"]; 

//Add Gesture Recognizer 
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; 
tapped.numberOfTapsRequired = 1; 
[cell.imageView addGestureRecognizer:tapped]; 
cell.imageView.userInteractionEnabled = YES; 
//[cell addSubview:cell.imageView]; 

return cell; 

} 

//Method controlling what happens when cell's UIImage is tapped 
-(void)imageTapped:(UIGestureRecognizer*)gesture 
{ 

UIImageView *selectedImageView=(UIImageView*)[gesture view]; 

UIImage *imageDefault = [UIImage imageNamed:@"checkmark.png"]; 
UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"]; 
UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"]; 

if (selectedImageView.image == imageDefault) { 
    selectedImageView.image = imageGreen; 
    //cell.selected = true; 
    NSLog(@"Selected"); 
} else { 
    selectedImageView.image = imageRed; 
    //cell.selected = false; 
    NSLog(@"Deselected"); 
} 
} 
+0

完美的作品!謝謝@pawan! – jeremytripp

相關問題