2015-10-07 30 views
0

我是iOS開發新手,我想知道當我點擊表格行時,我的數據顯示在表格視圖中應該選擇標籤。我的標籤有名稱城市。桌面視圖標籤上顯示的TableView數據

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

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

    return cell; 
} 
+0

你知道tableView'didSelectRowAtIndexPath'方法嗎? –

+0

可能是我的格式錯誤的方式,但我的問題是真的..有沒有人可以幫忙? –

+0

我知道這件事,但我不知道里面寫了什麼? –

回答

1

在viewDidLoad中添加此陣列

dataArr=[[NSMutableArray alloc]initWithObjects:@"Hyderabad",@"Bangalore",@"Chennai",@"Pune",@"Mumbai", nil]; 


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

{ 
    NSString *[email protected]"cellID"; 

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reUseid]; 

    if (cell==nil) 

    { 

     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reUseid]; 

    } 

    cell.textLabel.text = dataArr[indexPath.row]; 
    return cell; 
} 

這裏是TableView中didSelectRowAtIndexPath方法方法。

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

{ 

    NSLog(@"selected is %@",[dataArr objectAtIndex:indexPath.row]); 

    [self performSegueWithIdentifier:@"viewToDescriptionSegue" sender:self]; 

} 
在這種方法中,你從你可以得到選中的行狀 indexpath.rowindexpath

,並從,你可以從陣列獲取的對象。

0

有一個名爲didSelectRowAtIndexPath的方法UITableViewDelegate,它在用戶選擇任何行時被調用。從indexPath.row中獲取數組中的值並設置標籤上的文本。

您的項目實現下面的代碼: -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *name = [arrayname objectAtIndex:indexPath.row]; 
    [yourLabelInstance setText:name]; 
} 
0

這是UITableView的委託方法,當你在電池接頭被調用。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    city.text = [arrayname objectAtIndex:indexPath.row]; 
} 
+0

感謝它的工作... –

+0

還有一件事......我可以問一個更多的問題嗎?如果你讓我放? –

+0

是的什麼問題? – Indrajeet

0

以下是UITableView的委託方法,當您觸摸/選擇表中的任何行時調用。

在這裏,使用indexPath參數,您可以訪問當前選定的行索引。您可以從具有選定行索引的數據源數組中訪問數據。

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

    NSString *strCity = [arrCity objectAtIndex:indexPath.row]; 
    [lblCity setText:strCity]; 

}