2012-09-26 15 views
19

Mam przycisk i tabelę. Teraz chcę kliknąć w taki sposób, że Ilekroć wybiorę dowolny wiersz w tableview i naciśnij przycisk, nastąpi to konkretne wydarzenie naciśnięcia przycisku. W tym celu, po pierwsze mam dać tag do każdego wiersza tjKliknięcie zdarzenia w widoku UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *LabelCellIdentifier = @"cell"; 
UITableViewCell *cell; 
cell = [tableView dequeueReusableCellWithIdentifier:LabelCellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LabelCellIdentifier]; 


} 

if (indexPath.row <= [_arrayMp3Link count]) { 

    cell.textLabel.text = [_arrayMp3Link objectAtIndex:indexPath.row]; 

    } 

// now tag each row 
NSInteger count = 1; 
for (NSInteger i = 0; i < indexPath.section; i++) { 
    count += [[tableView dataSource] tableView:tableView numberOfRowsInSection:i]; 
} 
count += indexPath.row; 
// dequeue, create and configure... 
cell.tag = count; 



return cell; 
} 

a teraz w oddanie zdarzenie w przycisk po zaznaczeniu wiersza i naciśnij przycisk moje. Ale nie dostaniesz poprawnych rzeczy.

(IBAction)doDownload:(id)sender { 
// to select first row 
if(cell.tag==1) 
{ 
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
} 
+0

można uzyskać wiersz w IBAction. Sprawdź moją odpowiedź tutaj: http://stackoverflow.com/a/12594183/1144632 – danielbeard

Odpowiedz

29

Zadeklaruj zmienną int globalnie -

int rowNo; 

Następnie przypisać wartość do niego w didSelectRowAtIndexPath: metody

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    rowNo = indexPath.row; 
} 

Teraz masz indexNo. wybranego wiersza.

-(IBAction)doDownload:(id)sender 
{ 
    //Now compare this variable with 0 because first row index is 0. 
    if(rowNo == 0) 
    { 
     [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
    } 
} 
+0

ok dzięki! i jeśli chcesz tego samego działania dla każdego wiersza ... muszę użyć indeksu path.section ?? i co należy pobrać? – Christien

+0

Ile masz sekcji? – TheTiger

+0

tylko jeden ....... – Christien

5

Musisz użyć metody funkcja didSelectRowAtIndexPath z Tableview.

w tej metodzie zapisujesz wybrany znacznik wiersza lub cokolwiek chcesz zapisać. W akcji przycisku sprawdź wartość zapisanego obiektu i wykonaj dowolną czynność.

+0

k ale aby wybrać wiersz i nacisnąć przycisk, który rzeczą muszę być użyty – Christien

+0

jeśli chcesz akcji po wybraniu wiersza tabeli, co jest lepsze, możesz sprawdzić tag komórki wewnątrz funcji didiSelectRowAtIndexPath i zrobić to, co chcesz. Ale jeśli musisz użyć wyboru i przycisk naciśnij oba, a następnie użyj obu. – tausun

2
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(indexPath.row==i) 
{//Perform whatever action you would like for whichever row number..i'm just using i as an int placeholder 
    [[UIApplication sharedApplication]openURL:[NSURLWithString:@"http://www.google.com"]]; 
} 

//Or you could perform the same action for all rows in a section 
if(indexPath.section==i) 
{ 
[[UIApplication sharedApplication]openURL:[NSURLURLWithString:@"http://www.google.com"]]; 

} 
+0

ya thanx man! ale chcę, ilekroć wybiorę wiersz i kliknę przycisk, który podałem osobno. – Christien

+0

Przepraszam, nie rozumiem. Chcesz powiedzieć, że chcesz zrobić to samo, jeśli klikniesz przycisk i wiersz? Lub co próbujesz zrobić? –

+0

thanx. Mam to. Proszę sprawdzić, czy użyłem właściwego kodu do oznaczenia każdego wiersza. – Christien

3

pomocą funkcji źródła danych z UITableView który

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

które indexPath.row jest wskaźnikiem każdy rząd jest.

+0

ya thanx mam! ale chcę, ilekroć wybiorę wiersz i kliknę przycisk, który podałem osobno – Christien

+0

Po wybraniu wiersza, chcesz go również wybrać? Czy mam rację? – KarenAnne

1
//use selectedrow to check the condition 

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

        selectedrow=indexPath.row; 
    } 

     -(IBAction)doDownload:(id)sender { 
     // to select first row 
     if(selectedrow==1) 
     { 
      [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
     } 
+0

dzięki! to naprawdę działa dla mnie! – Christien

2

'wywiad-the-2.jpg' jest nazwa pliku obrazu

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 

    ViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewc"]; 

    [self.navigationController pushViewController:vc animated:YES]; 
    [vc setImageName:@"the-interview-2.jpg"]; 
} 
Powiązane problemy