2012-03-14 10 views
6

Próbuję zaimplementować aplikację opartą na UITableView. Aby wybrać UITableViewStyle jest Group.In moim TableView ich jest sekcji każdej sekcji posiadającej 1 wiersz.Dlaczego sprawdzamy, czy (komórka == zero) w 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; 
    } 

} 

Chcę dodać UITextView w sekcji

Do tego zrobiłem następujący kod

- (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; 
} 

W initil etapie widok tabeli jak ten

enter image description here

jeśli przewijam widok tabeli w górę iw dół, to zmieniono sekcję uitextView i wyświetlono wiele lokalizacji.

enter image description here

Nie mogę zrozumieć, moja wina, dlaczego tak się stało?

jeśli jestem wykonawczych powyższy kod na stronie if (komórka == 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 nie disply, myślę, że nie jest przydzielenie.

więc co stanowi różnicę pomiędzy kodu wykonawczego jeśli (komórka == ml) { wewnątrz }

jeśli (komórka == ml) { } z boku

+0

różnica polega na tym, że kiedy napisałeś kod w komórce == zero, będzie działać tylko raz na początku, jeśli napiszesz, to stworzy za każdym razem, gdy obciążenie widoku –

+0

powodowało zmianę wartości przekroju podczas przewijania widoku tabeli. czy mogę napisać kod na bok? –

+0

jeszcze jedna rzecz, którą możesz zrobić w swoim kodzie, ustaw jedną wartość tagu dla widoku tekstowego, a następnie napisz ten kod [[cell.contentView viewWithTag: 100 + indexPath.row] removeFromSuperview]; 100 + index.row jest wartością zmiennej –

Odpowiedz

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

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

ten może być stosowany zamiast

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

jest to prosty przykład napisać wewnątrz komórki == 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; 
    } 

myślę będzie wystarczająco prawo wyłącznie

+0

Czy jest możliwe utworzenie UITextView wewnątrz, jeśli (komórka == zero) –

+1

sprawdź powyższy kod teraz działa również dobrze –

+0

czy to działa dla Ciebie, czy jeszcze bardziej potrzebne .....? –

3

test if (cell == nil) obsługuje przypadek, w którym nie ma komórek do ponownego użycia, aby je usunąć. W takim przypadku musisz utworzyć nową komórkę. Podczas tworzenia nowej komórki odpowiadasz za budowanie jej hierarchii widoku.

+0

jeśli nie masz nic przeciwko, możesz opracować trochę więcej .. –

+2

@MusthafaPP Komórki są poddawane recyklingowi, a nie tworzenie nowych. Powód: wydajność, szybkość. Najczęściej w [OOP] (https://en.wikipedia.org/wiki/Object-oriented_programming), w razie potrzeby tworzymy nowe instancje (obiekty). Ale tworzenie nowego obiektu ma trochę na głowie, zajmuje trochę czasu. W rzadkich przypadkach, takich jak UITableView, potrzebujemy maksymalnej wydajności. Recykling istniejącego obiektu, podczas resetowania jego kluczowych właściwości/zmiennych odpowiednich do świeżego użycia, oszczędza wystarczająco dużo czasu, aby zagwarantować kłopot. Jeśli masz komórkę, użyj jej ponownie. Inaczej, zrób nową instancję. –

Powiązane problemy