2010-07-20 15 views
7

Mam UITableView i chciałbym zastosować obraz tła do wszystkich komórek. Mój wzrost dla każdej komórki jest zmienny. Jak powinienem zrobić tworzenie obrazu tła?Jak ustawić obraz tła UITableViewCell o różnych wysokościach?

cell.contentView.backgroundColor = [UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]]; 
+0

to nie działa dla mnie (po zamocowaniu literówkę) w tle komórkowej. –

Odpowiedz

3

Jednym z pomysłów jest umieszczenie na nim symbolu UII z rozciągliwym obrazem. W ten sposób nie ma znaczenia, jaka jest wysokość wiersza, obraz może rozciągać się, aby dopasować.

Można też zrobić coś podobnego do tego, co Matt zrobił here with a gradient layer

+0

Czy możesz opisać rozciągliwy obraz? Czy muszę utworzyć obraz o wielkości 1x55 pikseli (tylko przykład), który automatycznie rozciągnie się na płaszczyźnie x lub czy potrzebny jest dodatkowy kod? –

+3

Po prostu dajesz obraz, a następnie mówisz, które części nie są rozciągliwe za pomocą '[[UIImage imageNamed: @" someBGImage.png "] stretchableImageWithLeftCapWidth: 5 topCapHeight: 5];' gdzie 5 pikseli to część, która nie jest " t rozciągliwe (jak obramowanie). Więcej informacji http://developer.apple.com/iphone/library/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/stretchableImageWithLeftCapWidth:topCapHeight: – iwasrobbed

2

W metodzie cellForRowAtIndexPath można barwienia

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


{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    {  
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 
// for cell background color 
    cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"tab2.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 

// for selection color  
    cell.selectedBackgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 
    return cell; 
} 
Powiązane problemy