2012-02-20 3 views
0

У меня есть большая проблема с UITableView, я хочу использовать метку внутри клетки, так что я использую этот метод сделать этоUITableView - данные, и если (! Клетка)

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

// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell, 
// otherwise, replace it with a button cell. 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 
else { 

} 

if (indexPath.section == 0) { 

    elemento = [array objectAtIndex:indexPath.row]; 

    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)]; 
    labelTitle.text = [elemento objectForKey:@"Titolo"]; 
    labelTitle.backgroundColor = [UIColor clearColor]; 
    labelTitle.textColor = [UIColor whiteColor]; 
    [cell addSubview:labelTitle]; 

} else { 

    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)]; 
    labelTitle.text = @"Read More"; 
    labelTitle.backgroundColor = [UIColor clearColor]; 
    labelTitle.textColor = [UIColor whiteColor]; 
    [cell addSubview:labelTitle]; 

} 


return cell; 

}

таким образом я могу видеть все данные на моем столе, но ярлык являются перекрывания, чем я пытаюсь использовать этот метод

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

// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell, 
// otherwise, replace it with a button cell. 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

    if (indexPath.section == 0) { 

     elemento = [array objectAtIndex:indexPath.row]; 

     UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)]; 
     labelTitle.text = [elemento objectForKey:@"Titolo"]; 
     labelTitle.backgroundColor = [UIColor clearColor]; 
     labelTitle.textColor = [UIColor whiteColor]; 
     [cell addSubview:labelTitle]; 

    } else { 

     UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)]; 
     labelTitle.text = @"Read More"; 
     labelTitle.backgroundColor = [UIColor clearColor]; 
     labelTitle.textColor = [UIColor whiteColor]; 
     [cell addSubview:labelTitle]; 

    } 

} 
else { 

} 

return cell; 

}

метка в порядке, но в этом случае я могу видеть на моей таблице только 5 данных, и эти 5 данных повторяются в течение некоторого времени ...

Например, если в первом случае на моем столе я вижу: 1 , 2,3,4,5,6,7,8,9,10, ... во втором случае я вижу: 1,2,3,4,5,1,2,3,4,5, 1,2,3,4,5, ...

где проблема?

ответ

1

добавить этот код

for (UIView *view in [cell.contentView subviews]) 
    { 
     [view removeFromSuperview]; 
    } 

перед тем

если (indexPath.section == 0) {

elemento = [array objectAtIndex:indexPath.row]; 

UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)]; 
labelTitle.text = [elemento objectForKey:@"Titolo"]; 
labelTitle.backgroundColor = [UIColor clearColor]; 
labelTitle.textColor = [UIColor whiteColor]; 
[cell addSubview:labelTitle]; 

В настоящее время вы добавить метку в ячейку и в следующий раз ячейка повторно используется. Этикетка все еще существует, и вы добавляете надпись поверх нее.

+0

ничего не делать .... не работает, этикетка перекрывается! – kikko088

+0

да .. еще кое-что вам нужно сделать, это добавить метку как '[cell.contentView addSubview: labelTitle];' не как cell addSubvuew – Shubhank

+0

вы гений! Спасибо!! : D – kikko088

0

Код, который вы отправили, указывает UITableViewCellStyleSubtitle как стиль ячейки, что означает, что каждая ячейка будет иметь текстовую метку и текстовую метку, соответствующую соответствующим свойствам textLabel и detailTextLabel. Поэтому нет причин для выделения дополнительных экземпляров UILabel. Вместо этого просто заполните свойства существующих меток text. Например, вы можете переписать свою реализацию следующим образом:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID]; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     cell.textLabel.textColor = [UIColor whiteColor]; 

    } 

    cell.textLabel.text = (indexPath.section == 0 ? 
          [array objectAtIndex:indexPath.row] : 
          @"ReadMore");  

    return cell; 
} 
+0

проблема заключается в том, что мне нужно добавить также изображение влево, и я могу сделать это только с добавлением метки и изображения ... Я могу сделать это с видом аксессуаров, но они только на right ... – kikko088

+0

С левой стороны вы видите изображение, которое вы можете получить через свойство 'imageView' вашего экземпляра' UITableViewCell'. – jlehr

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