您還可以設置您創建的每個按鈕的tag
財產時tableView: cellForRowAtIndexPath:
,那麼當被稱爲你的buttonTapped
事件,仰望sender
並找到其tag
。 UIView的tag
屬性僅用於解決這類問題。
如果您需要更多信息,您可以創建一個UIButton子類,用於存儲任何或所有關於相關歌曲的信息。再次,您在cellForRowAtIndexPath
期間設置該信息,以便在點按該按鈕時進行檢索。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// Dequeue a cell and set its usual properties.
// ...
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[playButton addTarget:self action:@selector(playSelected:) forControlEvents:UIControlEventTouchUpInside];
// This assumes you only have one group of cells, so don't need to worry about the first index. If you have multiple groups, you'll need more sophisticated indexing to guarantee unique tag numbers.
[playButton setTag:[indexPath indexAtPosition:1]];
// ...
// Also need to set the size and other formatting on the play button, then make it the cell's accessoryView.
// For more efficiency, don't create a new play button if you dequeued a cell containing one - just set its tag appropriately.
}
- (void) playSelected:(id) sender;
{
NSLog(@"Play song number %d", [sender tag]);
}
謝謝!我要去檢查一下。 – 2012-04-07 16:31:51