2014-07-05 21 views
0

我不確定這是否可能。在uitableview單元內設置一個自定義視圖的代理

所以我有一個自定義單元格 - 裏面包含一個自定義的uiview。這個UIView是一個顯示條形圖的視圖。所以我想設置dataSource,以及該UIView的委託。

如果我使用該代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *simpleTableIdentifier = @"StatsCell"; 

    StatsTableViewCell *cell = (StatsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StatsTableViewCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 



    cell.barChart.dataSource = self ; 
    dell.barChart.delegate = self; 
} 

我得到一個警告:

Assigning to 'id<JBBarChartViewDelegate>' from incompatible type 'StatsViewController *const __strong' 

我該怎麼辦呢?然後,如果這是可能的 - 因爲委託方法將被調用的任何單元格 - 我怎麼區分哪些被調用?

我使用JBChartView從here

謝謝。

+0

爲什麼不讓單元格成爲它的子委託和數據源? –

+0

@ Lukasz'Severiaan'Grela你能舉個例子嗎?我不知道我明白你的意思。 – ghostrider

+0

例如在'StatsTableViewCell'的'awakeFromNib'或類似的設置'self.barChart.delegate = self;' –

回答

1

JBBarChartViewdelegate屬性的類型id<JBBarChartViewDelegate>。爲了設置對象是代表沒有得到這樣的警告,你必須定義一個類爲符合JBBarChartViewDelegate協議這樣:

@interface YourClass <JBBarChartViewDelegate> 

一旦你這樣做,你會看到警告任何@required協議如果有的話,你的類沒有實現的方法。你需要實現這些方法。

正如@ Lukasz'Severiaan'Grela建議,單元格可能更適合作爲委託/數據源而不是tableview。


你也得到由鑄造對象爲擺脫這樣的警告:

cell.barChart.delegate = (id<JBBarChartViewDelegate>)self; 

但我會強烈建議反對這一點。如果self未被定義爲符合協議,您將不會收到關於缺少@required協議方法的警告,因此您可能會遇到「未知選擇器」崩潰。

相關問題