2013-03-27 11 views
15

Mam XIB plik TimerCell.xib z UITableViewCell. W drugiej klasie w cellForRowAtIndexPath zainicjować ten UITableViewCell:Jak ustawić akcję dla UIButton w UITableViewCell

static NSString *CellIdentifier = @"cellTimer"; 
    TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 

w moim TimerCell Mam dwa UILabel i jeden UIButton. Dla tego przycisku chciałbym ustawić akcję dla pewnej metody.

Jak mogę to zrobić? A jak wyświetlić w pierwszym momencie dane z mojego licznika czasu w tle w czasie rzeczywistym?

Odpowiedz

25

Ten fragment kodu pomoże Ci

static NSString *CellIdentifier = @"cellTimer"; 
TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.tag=indexPath.row; 
    [button addTarget:self 
     action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; 
    [button setTitle:@"cellButton" forState:UIControlStateNormal]; 
    button.frame = CGRectMake(80.0, 0.0, 160.0, 40.0); 
    [cell.contentView addSubview:button]; 
    } 

    return cell; 
} 


-(void)aMethod:(UIButton*)sender 
{ 
NSLog(@"I Clicked a button %d",sender.tag); 
} 

Hope this helps !!!

+0

to działa! Dziękuję Ci!! Jak teraz ustawić tag? Zanim to było ** cell.staticButton.tag = indexPath.row; ** – Romowski

+0

Sprawdź moją zredagowaną odpowiedź @Romowski – Dany

-3

Proszę zrobić lepsze badania przed wysłaniem na SO. Oto kod, aby dodać przycisk do komórki

UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[Button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside]; 
[Button setTitle:@"ButtonTitle" forState:UIControlStateNormal]; 
Button.frame = CGRectMake(100.0, 10.0, 40.0, 40.0); 
[cell.contentView addSubview:Button]; 
+0

Dlaczego jest odrzucana? Proszę wyjaśnić lub jest to bezużyteczne. Czy to źle? Czy odpowiada lub próbuje odpowiedzieć na pytanie? – YSC

17

Skoro masz podklasę UITableViewCell nazwie TimerCell można dodać właściwości wylot TimerCell. Na przykład:

@interface TimerCell : UITableViewCell 

@property (nonatomic, strong) UIButton *button; 
@property (nonatomic, strong) UILabel *label; 

@end 

W TimerCell.xib podłącz gniazda do przycisku i etykiety.

Następnie w tableView:cellForRowAtIndexPath:, można łatwo uzyskać dostęp do przycisku, aby ustawić swój cel i działanie:

static NSString *CellIdentifier = @"cellTimer"; 
TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

[cell.button addTarget:self action:@selector(cellButtonWasTapped:) 
    forControlEvents:UIControlEventTouchUpInside];; 

można otworzyć etykietę komórki przy użyciu jego label własność zaktualizować go, gdy pożary czasomierza.

Innym sposobem na uzyskanie przycisku i etykiety jest użycie znaczników widoku, co opisano w artykule this answer.

W cellButtonWasTapped: (lub jakakolwiek akcja, którą wyślesz z przycisku), prawdopodobnie będziesz szukał ścieżki indeksu komórki zawierającej przycisk. Wyjaśniam, jak to zrobić w this answer.

+0

Nie rozumiem, jak mogę wyświetlić bieżącą wartość zegara w UITableViewCell ... czy możesz to wyjaśnić ponownie. Aby uzyskać więcej informacji: timer działa w trybie pojedynczym. Jest tylko 1 timer, ale istnieje słownik z kilkoma pozycjami, które zawierają wartości czasu. Każdy minutnik zmniejsza 1 ze wszystkich pozycji w słowniku. TimerCell jest komórką tableView, która pojawia się w wyskakującym okienku. Więc chciałbym pokazać wartości czasu elementu słownika w moim cell.label ... Dzięki – Romowski

+0

http://stackoverflow.com/questions/15653729/how-to-show-nstimers-time-value-in-uitableviewcells-label – Romowski

+0

+1 za świetne wyjaśnienie i dokumentację ... 10 więcej, aby przejść z tobą 119 000;) – logixologist

3
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"cellTimer"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
    NSInteger index = indexPath.row; 
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:101]; 
    UIButton *button = (UIButton *)[cell viewWithTag:100]; 
    [button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside]; 
} 

i ustawić UIButton and UILabel za tag w pliku XIB TimerCell.xib

Powiązane problemy