Programowo tworzę widok tabeli w ramach celu c. Jak mogę programowo ustawić komórki statycznie?Programowo ustawianie komórek statycznych w uitableview
Dzięki
Programowo tworzę widok tabeli w ramach celu c. Jak mogę programowo ustawić komórki statycznie?Programowo ustawianie komórek statycznych w uitableview
Dzięki
za pomocą komórek odrębny identyfikator dla każdego z nich można dostać. Można użyć czegoś podobnego:
Tworzenie programowych programów statycznych nie ma większego sensu. Komórki statyczne są w zasadzie tylko dla Konstruktora interfejsu i wymagają, aby cały TableView był statyczny. Pozwalają one przeciągnąć UILables, UITextFields, UIImageViews itp. Bezpośrednio do komórek i pokazać, jak wygląda w Xcode po uruchomieniu aplikacji.
Jednak twoje komórki mogą być "statyczne" programowo, ponieważ nie używają zewnętrznego źródła danych i nie kodują wszystkiego, co zwykle jest rodzajem bałaganu i generalnie kiepskim pomysłem.
Sugeruję utworzenie nowego kontrolera UITableViewController z .xib i dostosowanie go stamtąd, jeśli chcesz "statyczne" komórki. W przeciwnym razie po prostu zakoduj wszystkie wartości i to w zasadzie to samo, ale prawdopodobnie jest to kiepski projekt, jeśli można go uniknąć.
Można również zrobić to w starym stylu i po prostu utworzyć komórkę tak, jak chcesz w zależności od NSIndexPath
, działa to w przypadku Static Cell TVC i zwykłych widoków tabel (nie zapomnij zwrócić odpowiedniej liczby sekcji i wierszy w swoich metodach DataSource):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch indexPath.row {
case 0:
// First cell, setup the way you want
case 1:
// First cell, setup the way you want
}
// return the customized cell
return cell;
}
i chcesz utworzyć strukturę komórek, na przykład na ekranie ustawienia lub coś podobnego i może trzeba tylko zmodyfikować niektóre komórki treść, ale nie można przeciążać ich liczba lub sekcje struktura metoda twojej podklasy UITableViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
// Configure the cell...
if ([aCell.reuseIdentifier isEqualToString:@"someIdentifier"]){
//some configuration block
}
else if ([aCell.reuseIdentifier isEqualToString:@"someOtherIdentifier"]) {
//other configuration block
}
return aCell;
}
Ale możesz zrobić to lepiej, z odrobiną kodu;
1) Na początku pliku .m dodać typedef:
typedef void(^IDPCellConfigurationBlock)(UITableViewCell *aCell);
2) dodać do swojej własności cellConfigurations TablviewControllerSubclass przedłużeniem:
@interface IPDSettingsTableViewController()
@property (nonatomic, strong) NSDictionary *cellConfigurations;
@property (nonatomic) id dataModel;
@end
3) Modyfikowanie statyczne komórek TableviewController podklasy w scenorysie lub xib i dodaj unikalny identyfikator cellReuseIdentifier dla każdej komórki, którą chcesz programowo zmodyfikować:
4) W swoim viewDidLoad konfiguracji metoda cellsConfiguration bloków:
- (void)viewDidLoad
{
[super viewDidLoad];
[self SetupCellsConfigurationBlocks];
}
- (void)SetupCellsConfigurationBlocks
{
//Store configurations code for each cell reuse identifier
NSMutableDictionary *cellsConfigurationBlocks = [NSMutableDictionary new];
//store cells configurations for a different cells identifiers
cellsConfigurationBlocks[@"someCellIdentifier"] = ^(UITableViewCell *aCell){
aCell.backgroundColor = [UIColor orangeColor];
};
cellsConfigurationBlocks[@"otherCellIdentifier"] = ^(UITableViewCell *aCell){
aCell.imageView.image = [UIImage imageNamed:@"some image name"];
};
//use waek reference to self to avoid memory leaks
__weak typeof (self) weakSelf = self;
cellsConfigurationBlocks[@"nextCellIdentifier"] = ^(UITableViewCell *aCell){
//You can even use your data model to configure cell
aCell.textLabel.textColor = [[weakSelf.dataModel someProperty] isEqual:@YES] ? [UIColor purpleColor] : [UIColor yellowColor];
aCell.textLabel.text = [weakSelf.dataModel someOtherProperty];
};
weakSelf.cellConfigurations = [cellsConfigurationBlocks copy];
}
5) przeciążanie tableView: metodę cellForRowAtIndexPath takiego:
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
// configure cell
[self configureCell:aCell withConfigurationBlock:self.cellConfigurations[aCell.reuseIdentifier]];
return aCell;
}
- (void)configureCell:(UITableViewCell *)aCell withConfigurationBlock:(IDPCellConfigurationBlock)configureCellBlock
{
if (configureCellBlock){
configureCellBlock(aCell);
}
}
To jest dość powszechne chce zbudować prosty stolik do wykorzystania jako menu lub formularz, ale używanie wbudowanego interfejsu API ze źródłem danych i przekazywanie wywołań zwrotnych nie ułatwia zapisu lub obsługi. Może być konieczne dynamiczne dodawanie/usuwanie/aktualizowanie niektórych komórek, więc samo używanie scenariuszy nie będzie działać.
Połączyłem MEDeclarativeTable, aby programowo budować małe tabele.Zapewnia źródło danych i deleguje na UITableView
. Kończymy się interfejsem API, w którym udostępniamy instancje sekcji i wierszy zamiast implementować metody źródła danych i delegowania.