2013-01-14 103 views
1

我的表視圖使用自定義的類來創建細胞的UITableView單元選擇

MyCustomCell.h

#import <UIKit/UIKit.h> 

@interface MyCustomCell : UITableViewCell 

@end 

MyCustomCell.m

#import "MyCustomCell.h" 
#import <QuartzCore/QuartzCore.h> 


@implementation MyCustomCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) 
    { 
     // Initialization code 

    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    self.selectionStyle = UITableViewCellSelectionStyleBlue; 

    // Configure the view for the selected state 
} 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    self.imageView.frame = CGRectMake(5,4,80,80); 
    self.imageView.layer.masksToBounds = YES; 
    self.imageView.layer.cornerRadius = 5.0f; 
    self.imageView.contentMode = UIViewContentModeScaleAspectFill; 
    float imgWidth = self.imageView.image.size.width; 
    if(imgWidth > 0) 
    { 
     self.textLabel.frame = CGRectMake(110,2,170,25); 
     self.detailTextLabel.frame = CGRectMake(110,18,200,25); 
     self.detailTextLabel.font = [UIFont boldSystemFontOfSize:12]; 
     self.detailTextLabel.alpha = 0.0; 
     self.textLabel.font = [UIFont boldSystemFontOfSize:12]; 
     self.textLabel.textColor = [UIColor colorWithRed:.2314 green:.3490 blue:.5961 alpha:1.0]; 


    } 
} 

@end 

,並使用它像這樣:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 


    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 


     cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     //cell.accessoryType = UITableViewCellAccessoryCheckmark; 



    } 

    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; 

    if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) 
    { 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btn setBackgroundImage:[UIImage imageNamed:@"select2x.png"] forState:UIControlStateNormal]; 
    [btn addTarget:self action:@selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    btn.frame = CGRectMake(110, 25, 63, 32); 
     //btn.tag = indexPath.row; 

     viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     viewProfile.frame = CGRectMake(190, 25, 93, 32); 
     //[viewProfile setTitle:@"View Profile" forState:UIControlStateNormal]; 
     [viewProfile setBackgroundImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal]; 
     [viewProfile addTarget:self action:@selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:viewProfile]; 

    [cell.contentView addSubview:btn]; 

    } 

    else 
    { 

     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [btn setBackgroundImage:[UIImage imageNamed:@"select.png"] forState:UIControlStateNormal]; 
     [btn addTarget:self action:@selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     btn.frame = CGRectMake(110, 25, 63, 32); 
     //btn.tag = indexPath.row; 

     viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     viewProfile.frame = CGRectMake(190, 25, 93, 32); 
     //[viewProfile setTitle:@"View Profile" forState:UIControlStateNormal]; 
     [viewProfile setBackgroundImage:[UIImage imageNamed:@"viewProfile.png"] forState:UIControlStateNormal]; 
     [viewProfile addTarget:self action:@selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:viewProfile]; 

     [cell.contentView addSubview:btn]; 


     } 

    return cell; 

} 

現在每個單元格包含兩個按鈕,它們分別執行一個動作(我不會在單元格按鈕上進行操作,但是在按鈕上點按),就像上面創建的那樣

現在我想要的是當用戶點擊按鈕(如選擇按鈕)一個特定的單元格,那個特定的單元格只應該被突出顯示或着色或着色,並且當用戶點擊另一個單元格中的按鈕時,該單元格應該變亮,使得前一個單元格不被突出顯示。我曾嘗試在2個不同的變量中存儲前一個和下一個indexpath,但無法在它們上面實現我想要的功能。

- (void)SelectButtonTapped:(UIButton *)button 
{ 


    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 

    NSIndexPath *indexPath = [homeTable indexPathForCell:cell]; 

    // UITableViewCell *cell1 = [homeTable cellForRowAtIndexPath:indexPath]; 

    //currentSelection = indexPath.row; 

    //MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath]; 

    [[homeTable delegate]tableView:homeTable didSelectRowAtIndexPath:indexPath]; 

    //MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath]; 

    /*if(currentSelection == 0) 
    { 
     [cell1 setSelected:YES animated:YES]; 
     currentSelection++; 
    } 
    else 
    { 
     [cell1 setSelected:YES animated:YES]; 

    }*/ 
    // NSIndexPath *indexPath1 = [homeTable indexPathForCell:cell]; 

    NSLog(@"current===>%d",currentSelection); 



    //[cell1 setSelected:NO animated:YES]; 

    //if(currentSelection == indexPath) 




    //UIButton *clickButton = (UIButton *)sender; 
    // NSIndexPath *indexPathHighlight = [NSIndexPath indexPathForRow:button.tag inSection:0]; 

    //button.tag = indexPathHighlight.row; 





    //UITableViewCell *newCell = [homeTable cellForRowAtIndexPath:indexPathHighlight]; 


    //if(newCell.selected == NO) 
    //{ 
    //[newCell setSelected:YES animated:YES]; 
    // [newCell setBackgroundColor:[UIColor lightGrayColor]]; 
    //} 
    //else 
    // { 
     //[newCell setSelected:NO animated:YES]; 
     //[newCell setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:237.0f/255.0f blue:240.0f/255.0f alpha:1.0f]]; 
    //} 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath]; 

    cell1.selectionStyle= UITableViewCellSelectionStyleBlue; 

} 

你可以看看我凌亂的按鈕操作方法。我嘗試了很多選擇,但沒有成功。請幫助。

+0

粘貼完整的代碼將不會幫助,你指出的地方,你認爲問題是什麼?你嘗試過調試嗎? – rishi

+0

是的,我試過了,我得到的是當單元格中的按鈕被點擊時,單元格變爲高亮顯示,但是當我點擊另一個單元格中的按鈕時,也會變成高亮顯示,而不會使前一個單元格不亮,當我滾動表格視圖時,細胞進入正常未着色狀態 – user1845209

+0

您可以發佈didSelectRow方法的代碼。 – rishi

回答

2

取消對該行:

btn.tag = indexPath.row+1000; 

也改變了小區選擇風格

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 

時創建它。

在SelectButtonTapped方法:

- (void)SelectButtonTapped:(UIButton *)button 
{ 
    int buttonIndex = button.tag; 

    for (int row = 0; row<numberOfRows; row++) 
    { 
    NSIndexPath indexPathHighlight = [NSIndexPath indexPathForRow:row inSection:0]; 

    UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:indexPathHighlight]; 

    if (row == buttonIndex - 1000) 
    { 
     [newCell setSelected:YES animated:YES]; 
    } 
    else 
    { 
     [newCell setSelected:NO animated:YES]; 
    } 
    } 
} 
+0

當我取消註釋btn.tag = indexPath。行;和滾動表,編譯器拋出異常「NSInvalidArgumentException」,原因:' - [UIButton setText:]:無法識別的選擇器發送到實例: UILabel * lblTemp1 =(UILabel *)[cell viewWithTag:1]; Home * tempHomeObj =(Home *)[profileArray objectAtIndex:indexPath.row]; NSString * cellValue =(NSString *)tempHomeObj.userName; lblTemp1.text = cellValue; – user1845209

+1

@ user1845209將標記更改爲一些相關的標記,如indexPath.row + 1000 ..因爲您可能對另一個元素使用了相同的標記。 –

+0

好吧我嘗試了您的方法,但它使所有單元格突出顯示,即它不會使以前的單元格unhighlighted – user1845209

0

試試這個 -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath]; 
    cell1.selectionStyle= UITableViewCellSelectionStyleBlue; 
} 
+0

這隻會在點擊單元格區域以外的按鈕時纔會執行..我已經試過這個 – user1845209

+0

有沒有辦法在我的按鈕操作方法中調用didSelectRowAtIndexPath? – user1845209

2

讓我們覺得你的表只有一個部分,你的按鈕的標籤是相應的單元格的行數。

在按鈕動作

int rownumber = button.tag; 
//print the rownumber and see if you are getting the correct value 
UITableViewCell *cell = [tablename cellForRowAtIndexPath:[NSIndexPath  
indexPathForRow:rownumber inSection:0]]; 

[cell setHighlighted:YES]; 
相關問題