2011-07-20 39 views
1

我的代碼是「的UITableView數據源必須從的tableView返回一個細胞:的cellForRowAtIndexPath:」

//定製表格視圖單元格的外觀。

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    /// labels - names of Cities /// 


    UILabel *lblCity = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 22)]; 
    lblCity.font = [UIFont systemFontOfSize:14]; 
    lblCity.backgroundColor = [UIColor clearColor]; 
    //lblCity.backgroundColor = [UIColor redColor]; 

    UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 22)]; 
    lblDate.font = [UIFont systemFontOfSize:14]; 
    lblDate.backgroundColor = [UIColor clearColor]; 
    //lblDate.backgroundColor = [UIColor redColor]; 

    UILabel *lblSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 22)]; 
    lblSchool.font = [UIFont systemFontOfSize:14]; 
    lblSchool.backgroundColor = [UIColor clearColor]; 
    //lblSchool.backgroundColor = [UIColor redColor]; 


    /// Labels for description of city events /// 


    UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 30)]; 
    lblEvent.font = [UIFont systemFontOfSize:12]; 
    lblEvent.backgroundColor = [UIColor clearColor]; 

    UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 30)]; 
    lblEventAtDate.font = [UIFont systemFontOfSize:12]; 
    lblEventAtDate.backgroundColor = [UIColor clearColor]; 

    UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 30)]; 
    lblEventAtSchool.font = [UIFont systemFontOfSize:12]; 
    lblEventAtSchool.backgroundColor = [UIColor clearColor]; 



    if(RequestType == 2) 
    { 

     UIImageView *imgEventLabel = [[UIImageView alloc]initWithFrame:CGRectMake(00, 00, 480, 22)]; 

     UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)]; 


     if(indexPath.row == 0) 

     { 
      static NSString *CellIdentifier = @"Cell11"; 

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

       lblCity.text = @"City" ; 
      // [cell addSubview:lblCity]; 

       lblDate.text = @"Date" ; 
      // [cell addSubview:lblDate]; 

       lblSchool.text = @"School" ; 
      // [cell addSubview:lblSchool]; 

       imgEventLabel.image = [UIImage imageNamed:@"city_date_place.png"]; 
      // [cell addSubview:imgEventLabel]; 



       [imgEventLabel addSubview:lblCity]; 
       [imgEventLabel addSubview:lblDate]; 
       [imgEventLabel addSubview:lblSchool]; 

       [cell.contentView addSubview:imgEventLabel]; 

      } 

      return cell; 
     } 

     if(indexPath.row == 1) 

     { 
      static NSString *CellIdentifier = @"Cell12"; 

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

       cell.selectionStyle=UITableViewCellSelectionStyleNone; 
       cell.backgroundColor=[UIColor clearColor]; 
       cell.textLabel.numberOfLines = 999; 


       lblEvent.text = @"Event in City"; 
       lblEventAtDate.text = @"Event on Date"; 
       lblEventAtSchool.text = @"Event at School"; 

       [viewDescription addSubview:lblEvent]; 
       [viewDescription addSubview:lblEventAtDate]; 
       [viewDescription addSubview:lblEventAtSchool]; 

       [cell.contentView addSubview:viewDescription]; 

      } 

      return cell; 


    } 


    } 

    // Configure the cell... 

    return cell; 
} 

我不知道哪裏出錯,請幫忙。

+0

您不必再回到小區,你已經回到它的狀態裏面了。 – Nitish

+0

什麼樣的錯誤你getting..please提供GDB statments如果崩潰.. – ajay

+0

尼蒂什沒有概率甚至返回兩次BEC在第一return語句的方法執行完畢 – ajay

回答

5

您的dequeueReusableCellWithIdentifier:第一次調用後沒有檢查零和創造新的一個,如果有重用隊列中沒有可用的細胞。

換句話說,如果有一個在複用隊列中沒有細胞,您的請求類型是不等於2和行不等於0或1,您的單元將不被創建。這會導致在控制檯中看到的異常。

0
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

當您根據條件創建單元格時,無需在開始時編寫該單元格。

在剛剛結束返回nil。因爲我覺得,當你執行你的代碼

0

對不起,我沒有足夠的積分,對您的問題發表評論必須有一些條件爲真。你有沒有嘗試使用自定義單元?自定義單元就像當前TableViewCells的修改。你幾乎可以做任何你想要的TableViewCells不提供任何內容。從我看到的,你想要3個標籤和1個圖像。一個普通的TableViewCell不會給你有3個標籤的能力。除非你自己創造它。以下是您的鏈接。我希望這是你的想法。

iPhone Custom Cell Tutorial

相關問題