Mam 4 sekcje w moim widoku tabeli, jak dodać nagłówek dla każdej sekcji piszę następujący kod, ale nie działa.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section {
if (section == 0)
return @"Tasks";
if (section == 1)
return @"Appointments";
if (section == 2)
return @"Activities";
if (section == 3)
return @"Inactivities";
}
2
Pooja
21 luty 2012, 13:03
3 odpowiedzi
Najlepsza odpowiedź
Użyj następującego kodu...
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *lbl = [[UILabel alloc] init];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setFont:[UIFont fontWithName:@"Arial" size:17]];
[lbl setTextColor:BROWN];
switch (section)
{
case 0:
lbl.text = @" Name";
break;
case 1:
lbl.text = @" Quantity";
break;
case 2:
lbl.text = @" Amount";
break;
}
return lbl;
}
4
HelmiB
21 luty 2012, 14:02
Czy policzyłeś sekcje?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [sections count]; //or 4 in your case
}
2
alecnash
21 luty 2012, 13:07
Sprawdź numer sekcji 4 lub nie i zmień ten kod na:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
NSString *returnValue = @"";
if (section == 0)
returnValue = @"Tasks";
else if (section == 1)
returnValue = @"Appointments";
else if (section == 2)
returnValue = @"Activities";
else if (section == 3)
returnValue = @"Inactivities";
return returnValue;
}
Inaczej wszyscy mają rację
1
kulss
21 luty 2012, 13:11
Podobne pytania
Nowe pytania
iphone
NIE używaj tego znacznika, chyba że adresujesz konkretnie iPhone'a i / lub iPoda touch firmy Apple. W przypadku pytań niezależnych od sprzętu użyj tagu [ios]. Więcej tagów do rozważenia to [xcode] (ale tylko jeśli pytanie dotyczy samego IDE), [swift], [objective-c] lub [cocoa-touch] (ale nie [cocoa]). Proszę powstrzymać się od pytań dotyczących iTunes App Store lub iTunes Connect. Jeśli używasz języka C #, oznacz tagiem [mono].