創建了一個TableView,然後創建了自定義的單元,其中放了一個Button。現在我想要實現,點擊索引的時候獲取tableview的索引內容,但是點擊了對應索引的按鈕之後,沒有給出索引列表。
我的tableview類名是SubMenuViewController
,然後單元類名是SubMenuCell
,
SubMenuViewController
代碼如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ModelLocator *model = [ModelLocator getInstance];
static NSString *simpleTableIdentifier = @"SubMenuCell";
SubMenuCell *cell = (SubMenuCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SubMenuCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (model.foodSubItemList) {
FoodSubItemVO* foodSubItemTemp = [model.foodSubItemList objectAtIndex:indexPath.row];
[cell.lbSubFoodItem setText: foodSubItemTemp.foodSubItemName];
[cell.lbPrice setText: foodSubItemTemp.price];
[cell setIndexPath:indexPath];
}
return cell;
}
SubMenuCell
代碼:
- (IBAction)addItemIntoOrder:(id)sender {
NSLog(@"@%",indexPath);
NSLog(@"@%",indexPath.row);
}
在**SubMenuCell.h**文件中聲明了indexPath
你的NSLog有錯誤; 換成下面的代碼,注意 % 和@ 順序變了。
- (IBAction)addItemIntoOrder:(id)sender
{
NSLog(@"%@",indexPath);
NSLog(@"%@",indexPath.row);
}