2014-02-26 45 views
1

我已經以編程方式創建了UITableView,並且還以編程方式創建了包含在每個單元格/行中的UIButtons。我已經設置了這些按鈕,如果它們被輕敲,它們的按鈕圖像會改變。所以當用戶第一次看到表格視圖時,這些按鈕是灰色的,但是如果用戶點擊其中一個按鈕,那麼該按鈕會變成紅色。如果他們再次點擊它,它會變回灰色。當按下按鈕時,需要訪問UITableViewCell的detailTextLabel屬性

我需要這樣做,如果一個按鈕被按下,並且當前正在使用紅色圖像,那麼它會將其單元格的detailTextLabel屬性值傳遞給NSMutableArray對象。

這是我的代碼,控制大部分的UITableView。這是細胞的textLabels和detalTextLabels設置:

userName = [self.potentiaFriendsInParseFirstNamesArray objectAtIndex:indexPath.row]; 

NSString *firstNameForTableView = [self.potentiaFriendsInParseFirstNamesArray objectAtIndex:indexPath.row]; 

NSString *userNameForTableView = [self.potentiaFriendsUsernameArray objectAtIndex:indexPath.row]; 

UIImage *addUserButtonImage = [UIImage imageNamed:@"SliderThumb-Normal-G"]; 

UIImage *addUserButtonImageHighlighted = [UIImage imageNamed:@"SliderThumb-Normal"]; 

UIButton *addUserButton = [[UIButton alloc]init]; 

addUserButton.frame = CGRectMake(237, -10, 64, 64); 

[addUserButton setImage:addUserButtonImage forState:UIControlStateNormal]; 

[addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateHighlighted]; 

[addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateSelected]; 

[addUserButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; 

[cell.textLabel setText:firstNameForTableView]; 

[cell.detailTextLabel setText:userNameForTableView]; 

[cell.contentView addSubview:addUserButton]; 

上面的代碼中最重要的部分是這些語句:

[addUserButton setImage:addUserButtonImage forState:UIControlStateNormal]; 

[addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateHighlighted]; 

[addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateSelected]; 

以上控制按鈕的不同狀態的設置,這有助於使按鈕在按下時變成灰色圖像或紅色圖像。

下面的語句是一個方法調用處理「觸摸」事件,使從灰度圖像的紅色圖像按鈕改變當按下它:

[addUserButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; 

這裏是上述方法的方法在我的視圖控制器實現:

- (void)handleTouchUpInside:(UIButton *)sender { 
    sender.selected = !sender.selected; 

    NSLog(@"Has sender state changed?: %u", sender.state); 
} 

你會注意到我使用NSLog打印出按鈕的狀態。每次按下某個按鈕時,「狀態」屬性都會更改它的值。現在我需要弄清楚一種方法,以便如果特定按鈕的狀態發生變化,我們將抓住它的單元格的「detailTextLabel」屬性並將其放置在NSMutableArray中。

回答

1

可以使用addUserButton.tag屬性來保持的tableview索引行(1),所以裏面的handleTouchUpInside(2)你可以找到按鈕行:的的cellForRowAtIndexPath函數內部

1集indexPath行

//Keep indexPath.row on Button.tag 
addUserButton.tag = indexPath.row 

2-上的TableView行查找按鈕

- (void)handleTouchUpInside:(UIButton *)sender { 
    UIButton *cellButton = (UIButton *)sender; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:cellButton.tag inSection:0]; 

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    cell.detailTextLabel.text = @"teste"; 

} 

我希望它的作品。

+0

我加入你的代碼,它並沒有改變任何東西。 – user3344977

+0

您是否在addUserButton創建時將標籤屬性設置爲添加到單元格子視圖之前? – carantes

+0

你問我,如果我在我原來的代碼發佈,如果我添加標籤屬性到我的UIButton對象稱爲「addUserButton」? – user3344977

0

這裏是你如何繼承的UITableViewCell做到這一點的精華(濃縮了一下,以節省空間......)首先.H

#import <UIKit/UIKit.h> 

@interface SampleCell : UITableViewCell 
@property (nonatomic, assign) id delegate; 
@end 


@protocol FancyProtocolNameGoesHere 
- (void)doSomethingWithThisText:(NSString*)text fromThisCell:(UITableViewCell*)cell; 
@end 

而且。米

#import "SampleCell.h" 

@interface SampleCell() 

@property (strong, nonatomic) UIButton *button; 
- (void)handleTouchUpInside; 

@end 


@implementation SampleCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     [self configureCell]; 
    } 
    return self; 
} 

- (void)configureCell 
{ 
    _button = [[UIButton alloc] init]; 
    //Configure all the images etc, for the button 
    [_button addTarget:self action:@selector(handleTouchUpInside) forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:_button]; //you'd probably want to set the frame first... 
} 

- (void)handleTouchUpInside 
{ 
    if (_button.selected) { 
     [self.delegate doSomethingWithThisText:self.textLabel.text fromThisCell:self]; 
    } 
    _button.selected = !_button.selected; 
} 

@end 

註冊這個子類用的TableView(記得要聲明遵守該協議),並在-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath

指派代表自我的最後法寶當屬你實現- (void)doSomethingWithThisText:(NSString*)text fromThisCell:(UITableViewCell*)cell;,因爲你可以調用[self indexPathForCell:cell]您傳入的單元格參數以獲取您可能需要的索引路徑,以便您知道將文本插入NSMutableArray數據對象的位置。

一個子類的UITableViewCell有委託協議模式是不是這樣做的唯一途徑,但我認爲它有一定的道理對你所描述的。

0

該解決方案對錶格部分是安全的。

- (void)onButtonPressed:(UIButton *)sender event:(id)event 
{ 
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForItemAtPoint:point]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    /* use cell here */ 
} 
+0

我很抱歉,但我很困惑。 「安全表格部分」是什麼意思? – user3344977

+0

如果您將使用按鈕的標記值,則只能分配「行」。如果只知道行,如何檢測按下了哪個「節」和「行」?我的解決方案,你不知道'行'和'節'(完整索引路徑)檢測單元格。 – avdyushin