我在UITableView的每個單元格中都有一個按鈕。現在我想每個按鈕綁定到其操作方法在功能:如何在循環中設置UIButtons的動作?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ...}
這裏是我試圖代碼:
NSString* functionNamePrefix = @"actionMethod_";
NSString* functionName = [functionNamePrefix stringByAppendingString:[NSString stringWithFormat:@"%d",indexPath.row]];
SEL selectorFromString = NSSelectorFromString(functionName);
[button addTarget:self action:@selector(selectorFromString:) forControlEvents:UIControlEventTouchDown];
失敗是:行動選擇採取「selectorFromString」作爲一個函數的名字,但SEL我從字符串轉換。
有什麼建議嗎? TIA!
感謝,它works.But @jrturton的答案是做我想做的正確途徑。 – Maadiah 2012-07-24 02:04:51