行動我想選擇一個UITableView
每個單元,並執行與每個單元相關的動作。處理單個單元格選擇及其在UITableView的
例如如果我在一行中選擇第一個單元格,它應該引導我到一頁,如果我選擇第二個單元格,它應該引導我到其他頁面。如何可能?有人可以提供給我一個示例代碼?
行動我想選擇一個UITableView
每個單元,並執行與每個單元相關的動作。處理單個單元格選擇及其在UITableView的
例如如果我在一行中選擇第一個單元格,它應該引導我到一頁,如果我選擇第二個單元格,它應該引導我到其他頁面。如何可能?有人可以提供給我一個示例代碼?
試試下面的代碼,你可以使用UITableView的方法來實現這個:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0) // first cell
NSLog(@"Perform action of first cell selection");
else if(indexPath.row == 1) // second cell
NSLog(@"Perform action of second cell selection");
else if(indexPath.row == 2) // third cell
NSLog(@"Perform action of third cell selection");
else if(indexPath.row == 3) // fourth cell
NSLog(@"Perform action of fourth cell selection");
else if(indexPath.row == 4) // fifth cell
NSLog(@"Perform action of fifth cell selection");
else
NSLog(@"Perform action for all rows except first 5 rows");
return indexPath;
}
顯示錯誤「控制達到非void函數的末尾」 –
對不起@Akhil Jiji我這是錯字錯誤,我錯過了要添加返回語句。我已經更新了我的答案,現在它應該完美地工作。 – Aamir
你需要掛接到表視圖的委託方法,做在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
方法的動作。
去閱讀蘋果的guide on Table View。
你可以做的那些東西,在didSelectRowAtIndexPath方法 -
在的ObjectiveC -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0) // first cell
{
// Do your stuff for 1st cell selection;
}
else if(indexPath.row == 1)// second Cell
{
// Do your stuff for 2nd cell selection
}
//continue like this
}
在斯威夫特 -
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
如何指定選擇哪個單元格? –
你能做到這一點使用indexPath,這樣的事情 - 如果(indexPath.row == 0)//第一個單元格 //做你的第一小區選擇的東西; else if(indexPath.row == 1)second cell //做第二個單元格選擇的東西 –
此示例代碼可能對您有所幫助。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger index = indexPath.row;
switch (index) {
case 0:
// Initilal your first page and redirect to that
break;
case 1:
// Initilal your second page and redirect to that
break;
case 2:
// Initilal your three page and redirect to that
break;
case 3:
// Initilal your four page and redirect to that
break;
case 4:
// Initilal your five page and redirect to that
break;
default:
//do your default
break;
}
}
其他的答案是從根本上是正確的,但如果你想迅速解決:
performSegueWithIdentifier(identifier: segueIDs[indexPath.row], sender: self)
這裏segueIDs應該被定義爲包含您需要獲得所有SEGUE標識字符串數組到其他視圖,與表格單元格的順序相同。
舉個例子,如果
var segueIDs = ["first", "second", "third", "fourth", "fifth"]
然後按中間單元將帶您到屏幕,「第三」的鏈接。快樂的編碼。
告訴我你到底要什麼? –
@SudheerKolasani我有5個在我tableView.I希望在點擊每個你cell.Can告訴我一個示例代碼重定向到5個不同的頁面? –