2010-08-20 2 views
0

Это таблицаView, как вы можете видеть, какая ячейка имеет две части, левая, - leftUIView, а правая - rightUIView. Красный и зеленый цвета могут отображаться, но созданная вами rightLabel не может быть успешно показана. Что случилось с моим кодом? Спасибо.Почему текстовый файл не может загружаться в моей ячейке?

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

     static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleSubtitle 
       reuseIdentifier:SectionsTableIdentifier] autorelease]; 
    } 

    cell.textLabel.textColor = [UIColor whiteColor]; 
    UIView *rightUIView = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 160, cell.frame.size.height)]; 
    [rightUIView setBackgroundColor:[UIColor greenColor]]; 
    UIView *leftUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, cell.frame.size.height)]; 
    [leftUIView setBackgroundColor:[UIColor redColor]]; 

    UILabel *rightLabel = [[UILabel alloc] init]; 
    [rightLabel setText:@"dummy"]; 
    [rightUIView addSubview:rightLabel]; 

    [cell addSubView:leftUIView]; 
    [cell addSubView:rightUIView]; 

} 
+1

Этот код не отображает ни один из ваших объектов left/rightUIView. Отправьте код, который вы используете. Возможно, что-то не так с этим кодом, но без * оригинального * кода он просто догадывается. – Eiko

ответ

1

Первая проблема с вашим кодом является то, что вы создаете подвидов ячейки каждый раз, когда клетка испрашивается - если прокрутить таблицу туда и сюда вашей клетки будут иметь кучу этих подвидов - конечно, это не хорошо. Вы должны создать ячейки подвидов только один раз - при создании ячейки и после этого только установить соответствующие им значения:

const int kRightViewTag = 100; 
const int kRightLabelTag = 200; 
... 
if (cell == nil) { 
    // Create everything here 
    cell = [[[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleSubtitle 
      reuseIdentifier:SectionsTableIdentifier] autorelease]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 

    UIView *rightUIView = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 160, cell.frame.size.height)]; 
    rightUIView.tag = kRightViewTag; 
    [rightUIView setBackgroundColor:[UIColor greenColor]]; 
    [rightUIView release]; // Do not forget to release object you allocate!! 

    UIView *leftUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, cell.frame.size.height)]; 
    [leftUIView setBackgroundColor:[UIColor redColor]]; 
    [leftUIView release]; // Do not forget to release object you allocate!! 

    UILabel *rightLabel = [[UILabel alloc] initWithFrame:leftUIView.bounds]; 
    rightLabel.tag = kRightLabelTag; 
    [rightUIView addSubview:rightLabel]; 
    [rightLabel release]; 
} 
// Setup values here 
UILabel* rightLabel = (UILabel*)[[cell viewWithTag:kRightViewTag] viewWithTag:kRightLabelTag]; 
rightLabel.text = @"dummy"; 

Вторая вещь вы не установили рамку лейбла в любом месте (я установил это выше), так вероятно, имеет нулевой размер кадра, и поэтому вы не можете его увидеть.

Третья проблема заключается в том, что вы выделяете виды, но не выпускаете их, поэтому они просто течет. Не забывайте, что если вы создаете некоторые объекты с помощью alloc, new или copy, то вы несете ответственность за их освобождение.

Смежные вопросы