2012-06-11 41 views
0

我有兩個繼承自某個基類的類。在這些類中我有相同的tableview方法,所以我決定將所有的邏輯移動到基類。沒有nibs文件,全部在代碼中。但基類由於某種原因不響應tableview事件。將該邏輯移動到基類是否正確,還是存在一些已知問題,還是我在實現中丟失了某些東西?從實現tableview委託和數據源方法的基類繼承

我只是分配:

self.tableView.delegate = self; 
self.tableView.dataSource = self; 

然後執行:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ([indexPath section] == 0) { 
     return 320; 
    } 
    else { 
     return 0; 
    } 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 0) { 
     return [self.imagesArray count]; 
    } 
    else { 
     return 0; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellIdentifier = @"mediaCell"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) { 
     if ([indexPath section] == 0) { 
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0f, 320.0f)] autorelease]; 

      UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0f, 320.0f)]; 
      imageView.tag = 1; 
      [cell addSubview:imageView]; 
      [imageView release]; 
     } 
    } 

    NSURL *url = [NSURL URLWithString:[self.imagesArray objectAtIndex:[indexPath row]]]; 
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1]; 

    [imageView setImageWithURL:url]; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    if (section == 0) { 
     return self.filterBar.bounds.size.height; 
    } 
    else { 
     return 0; 
    } 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    if (section == 0) { 
     return self.filterBar; 
    } 
    else { 
     return 0; 
    } 
} 
+0

你可以把一些代碼。你如何定義它的代表?請註明 –

+0

您是否意外地在您的孩子班中留下了一些懸掛功能?按照面向對象的規則,子類將首先被​​檢查。如果a)在子類中沒有實現,或者b)子類調用'super',它只會調用基類實現。另外,您是否檢查過以確保表視圖在那個時候不是「nil」? – borrrden

+0

子類中沒有tableview方法的實現,並且uitableview不是零 – nik

回答

0

如果你指的是的tableview委託方法,我認爲你缺少添加的類別中的定義基類,你可以澄清,如果方法是由UITableview使用的代表?

相關問題