2014-09-01 47 views
0

我已經包含在NSMutableDictionnary產品的列表,我diplsay它是這樣的:檢測點擊的UITableView

http://imgur.com/rKA2yvA

現在,我想補充一個觀點每一個產品。如果我點擊「紫蘇」對於爲例我要切換視圖,並有一個新的觀點,產品細節或別的東西..

我添加以下代碼:

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

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath]; 

// Configure the cell... 
NSString *sectionTitle = [aromaSectionTitles objectAtIndex:indexPath.section]; 
NSArray *sectionAroma = [tableData objectForKey:sectionTitle]; 
NSString *aromaLabel = [sectionAroma objectAtIndex:indexPath.row]; 
cell.textLabel.text = aromaLabel; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
} 
cell.textLabel.userInteractionEnabled = YES; 
cell.textLabel.tag = indexPath.row; 

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonPressed:)]; 
tapped.numberOfTapsRequired = 1; 
[cell.textLabel addGestureRecognizer:tapped]; 
return cell; 
} 


-(void)buttonPressed :(id) sender { 
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender; 
NSLog(@"Tag = %ld", (long)gesture.view.tag); 
} 

在我的日誌ID,但它不是唯一的,id是產品的行號,這裏Armoise將是0,ArbreThé1,但是Basilic將是0並且Baie將會是1等等。 我該如何創建根據用戶的選擇查看?

非常感謝您的幫助!

+0

你只是想要一個細胞是可點擊?有一個委託方法。 tableView:didSelectRow:atIndexPath: – CW0007007 2014-09-01 12:46:05

+0

我想在描述產品的視圖上重定向用戶 – 2014-09-01 12:50:20

回答

3

可以使用的UITableViewDelegate方法

//表的觀點是選擇一個單元

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    //Your Code here 
} 
+0

因此,我將能夠顯示描述所選產品的視圖? – 2014-09-01 12:50:50

+0

是在這個方法中,你推新的viewcontroller。或者你可以隱藏取消隱藏其他視圖 – BHASKAR 2014-09-01 12:52:24

+0

而我該如何知道用戶選擇哪個產品。例如,您點擊Basilic,在我的新視圖中,我想展示一個basilic的圖像,讓我們說價格,我不想爲每個產品創建視圖,我希望它是自動的。可能嗎 ? – 2014-09-01 12:54:52