2012-03-14 11 views
6

Tôi đang cố triển khai ứng dụng dựa trên UITableView.Để tôi chọn UITableViewStyle là Group.Trong TableView của chúng, chúng là phần mỗi phần có 1 hàng.Tại sao chúng ta kiểm tra xem (cell == nil) trong UITableViewController?

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return 1; 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section==12) 
    { 
     return 120; 
    } 
    else 
    { 
     return 60; 
    } 

} 

Tôi muốn thêm một UITextView trên Mục

Cho rằng tôi đã mã sau

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

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



    } 
    if ([indexPath section] == 12) 
    { 
     if([indexPath row]==0) 
     { 
      descriptionTextField=[[UITextView alloc] initWithFrame:CGRectMake(5, 8, 290, 106)]; 
      descriptionTextField.font = [UIFont systemFontOfSize:15.0]; 
      descriptionTextField.backgroundColor=[UIColor scrollViewTexturedBackgroundColor]; 

      [descriptionTextField setDelegate:self]; 
      [descriptionTextField setTag:2]; 
      [descriptionTextField setText:@"Enter Location Description."]; 

      descriptionTextField.keyboardType=UIKeyboardTypeDefault; 
      descriptionTextField.returnKeyType=UIReturnKeyNext; 


      descriptionTextField.textColor=[UIColor blackColor]; 
      descriptionTextField.editable=YES; 
      descriptionTextField.autocapitalizationType=UITextAutocapitalizationTypeWords; 
      descriptionTextField.autocorrectionType=UITextAutocorrectionTypeDefault; 
      descriptionTextField.textAlignment=UITextAlignmentLeft; 

      UIToolbar* keboardToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)]; 

      UIBarButtonItem *extra=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
      UIBarButtonItem *Done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(keyboardDoneButtonActin:)]; 
      [Done setWidth:65.0f]; 
      [keboardToolBar setItems:[[[NSArray alloc]initWithObjects:extra,Done, nil]autorelease] ]; 
      [extra release]; 
      [Done release]; 

      [keboardToolBar setTintColor:[UIColor blackColor]]; 
      [keboardToolBar setAlpha:.70]; 
      [descriptionTextField setInputAccessoryView:keboardToolBar]; 
      [descriptionTextField setTag:101]; 
      [cell.contentView addSubview:descriptionTextField]; 
      [descriptionTextField release]; 
     } 
    } 

    return cell; 
} 

Trong initil sân khấu xem bảng như thế này

enter image description here

nếu tôi đang di chuyển tableview lên và xuống, thì phần uitextView thay đổi và nó sẽ hiển thị nhiều vị trí.

enter image description here

Tôi không thể hiểu lỗi của mình, tại sao điều này lại xảy ra?

nếu là thực hiện các mã trên trong một bên if (tế bào == nil)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

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

    if ([indexPath section] == 12) 
    { 
     if([indexPath row]==0) 
     { 
      **/* implemention all code here*/** 

      [cell.contentView addSubview:descriptionTextField]; 
      [descriptionTextField release]; 
     } 
    } 

    return cell; 

}

UITextView không disply, tôi nghĩ rằng nó không được phân bổ.

nên sự khác biệt giữa mã thực hiện trong if (tế bào == nil) { bên }

if (tế bào == nil) {} ra phía

+0

chỉ sự khác biệt là khi bạn đã viết mã bên trong ô == nil, nó sẽ chỉ hoạt động một lần khi bắt đầu nếu u viết ra nó sẽ tạo ra mỗi khi lượt xem –

+0

lý do giá trị phần thay đổi nếu cuộn chế độ xem bảng. nếu tôi wright code out side? –

+0

một điều nữa bạn có thể làm trong mã của bạn đặt một giá trị thẻ cho textview và sau đó viết mã này [[cell.contentView viewWithTag: 100 + indexPath.row] removeFromSuperview]; 100 + index.row là giá trị thẻ –

Trả lời

-2
NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row]; 

// Dequeue or create a cell of the appropriate type. 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

này có thể được sử dụng thay cho

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

đây là ví dụ đơn giản để viết bên trong tế bào == nill

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

static NSString *CellIdentifier = @"CellIdentifier"; 
// Dequeue or create a cell of the appropriate type. 
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell= nil; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    [[cell.contentView viewWithTag:100+indexPath.row] removeFromSuperview]; 

    UIView *selectview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 30)]; 
    [selectview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"strip_s12A-1_h.png"]]]; 
    cell.selectedBackgroundView = selectview; 
    [selectview release]; 

    UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 30)]; 
    //cellTitle.adjustsFontSizeToFitWidth=YES; 
    [cellTitle setBackgroundColor:[UIColor clearColor]]; 
    [cellTitle setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:17]]; 
    [cellTitle setTextColor:[UIColor blackColor]]; 
    cellTitle.tag = 100+indexPath.row; 

     cellTitle.text= [[[cellArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForKey:@"Des2"]; 

    [cell.contentView addSubview:cellTitle]; 

    [cellTitle release]; 
} 
return cell; 
    } 

tôi nghĩ rằng sẽ có đủ đúng

+0

Có thể tạo UITextView bên trong nếu (cell == nil) –

+1

kiểm tra mã trên ngay bây giờ làm việc tốt của nó quá –

+0

là điều này làm việc cho u hoặc bất kỳ cần thiết hơn .....? –

3

là gì kiểm tra if (cell == nil) xử lý trường hợp không có các tế bào tái sử dụng để khử, trong trường hợp đó bạn phải tạo một ô mới. Khi bạn tạo ô mới, bạn chịu trách nhiệm xây dựng hệ thống phân cấp khung nhìn của nó.

+0

nếu u không nhớ u có thể xây dựng ít hơn .. –

+2

@MusthafaPP Các tế bào được tái chế, thay vì sau đó instantiating cái mới. Lý do: hiệu suất, tốc độ. Thông thường nhất trong [OOP] (https://en.wikipedia.org/wiki/Object-oriented_programming), chúng tôi tạo các cá thể (đối tượng) mới khi cần thiết. Nhưng ngay lập tức một đối tượng mới có một số chi phí, mất một chút thời gian. Trong một số ít trường hợp, chẳng hạn như UITableView, chúng tôi cần tối đa hiệu suất. Tái chế một đối tượng hiện có, trong khi đặt lại các thuộc tính/biến quan trọng của nó phù hợp với mục đích sử dụng mới, tiết kiệm đủ thời gian để đảm bảo sự phiền toái. Vì vậy, nếu bạn có sẵn một ô, hãy sử dụng lại nó. Khác, tạo một ví dụ mới. –