2014-07-20 70 views
0

我必須類: existsUserView和existUserCustomCell。如何通過UIButton傳遞參數

的代碼existUserView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ExistUserCustomCell *cell = (ExistUserCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"ExistUserCustomCell"]; 

    KidManager *kid = [self.kidsArray objectAtIndex:indexPath.row]; 
    cell.kidName.text = kid.firstName; 
    if([kid.inside isEqualToString:@"1"]){ 
     cell.kidStatus. text = @"some string"; 
    }else{ 
     cell.kidStatus.text = @"some string"; 
    } 

    return cell; 
} 

代碼existUserCustomCell:

- (IBAction)reportMissing:(id)sender { 

} 

- (IBAction)callTeacher:(id)sender { 

} 

我需要從 'existUserView' 數據傳遞到 'existUserCustomCell' 兩個鍵上的功能,要知道行時我按他們。 我該怎麼做最好的方法?

+0

爲什麼你需要細胞知道它的行?這很少需要,如果有的話。 – rmaddy

+0

它是一個自定義單元格,即時通訊處理不同類.. – OshriALM

+0

這不能回答我的問題。 – rmaddy

回答

2

如果您需要將數據從視圖控制器傳遞給單元格,請向該單元類添加一個屬性(或兩個或三個)。當您在cellForRowAtIndexPath中設置單元格時,請設置該屬性。然後你的單元格的方法(包括按鈕處理程序)可以根據需要訪問數據。

一個屬性添加到您的類:

現在
@interface ExistUserCustomCell : UITableViewCell 

// add this to anything you have 
@property (nonatomic, strong) KindManager *kid; 

@end 

您的按鈕方式訪問:

- (IBAction)reportMissing:(id)sender { 
    // access self.kid to get data 
    // anything else you need 
} 
表視圖控制器

然後:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ExistUserCustomCell *cell = (ExistUserCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"ExistUserCustomCell"]; 

    KidManager *kid = [self.kidsArray objectAtIndex:indexPath.row]; 
    cell.kid = kid; 
    cell.kidName.text = kid.firstName; 
    if([kid.inside isEqualToString:@"1"]){ 
     cell.kidStatus. text = @"some string"; 
    }else{ 
     cell.kidStatus.text = @"some string"; 
    } 

    return cell; 
} 

我猜測這是您在單元中需要的KidManager數據。根據需要調整。

順便說一句 - 如果我的猜測是正確的,那麼單元應該用數據設置自己,而不是使用cellForRowAtIndexPath方法中的邏輯。

+0

即時通訊對不起,我是ios開發新手。正如我所說,我有2班。 一個處理表視圖和第二個處理tableview單元格(自定義單元格)。 我在單元格上有2個按鈕,所以在自定義單元類中的IBAction。 我需要將數據分別傳遞給IBAction函數給單元格indexPath。我怎樣才能做到這一點? – OshriALM

+1

我已經告訴你如何。如果您爲單元類中的數據添加屬性,則單元中的按鈕方法可以訪問數據。查看我更新的答案和更多詳細信息。 – rmaddy