J'essaie d'utiliser un UITableView
sans utiliser une pointe et sans utiliser un UITableViewController
.
J'ai ajouté une instance UITableView
à un UIViewController
Like So
mytable = [[UITableView alloc] initWithFrame:CGRectMake(22, 207, 270, 233)]; [mytable setDelegate:self]; [self.view mytable];
Aussi j'ai ajouté les methods de vue de table suivantes à mon UIViewController
(couper pour des raisons de concision)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController
un avertissement disant que mon UIViewController
pas le protocole de délégué UITableView
.
Quelle est la bonne façon de dire à la table où ses methods de délégué sont?
(Ceci est ma première tentative d'utilisation d'un UITableView
sans sélectionner le controller UITableTableview
partir des nouvelles options de file)
Vous devez conformer votre class aux protocoles UITableViewDelegate
et UITableViewDataSource
. ( cellForRowAtIndexPath:
trouve dans le protocole UITableViewDataSource
)
Pour ce faire, utilisez des chevrons dans votre définition d'interface de class:
@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {...}
La raison pour laquelle vous recevez cet avertissement maintenant et pas avant lorsque vous utilisiez UITableViewController
est que UITableViewController
est déjà conforme à ces protocoles.
Ainsi, un UITableViewController
est essentiellement une class qui se conforme à UITableViewDelegate
et UITableViewDataSource
, et a un membre d'instance UITableView
. C'est à peu près tout.
Bien sûr, si vous ne sous- UITableViewController
pas UITableViewController
, vous devez configurer manuellement la dataSource
et le delegate
de UITableView
:
[tableView setDelegate:self]; [tableView setDataSource:self];
Vous devez également définir le délégué dataSource sur self:
[tableView setDelegate:self]; [tableView setDataSource:self];
ou, également:
tableview.delegate = self; tableview.dataSource = self;
L'avertissement vous UITableViewDelegate
simplement que votre section UITableViewDelegate
doit déclarer que vous implémentez le protocole UITableViewDelegate
:
@interface MyUIViewController : UIViewController < UITableViewDelegate >
[tableview setDataSource:self]
Vous devez le donner définitivement, Pourquoi à cause des deux methods requirejses de UITableView
sont sous le protocole UITableViewDataSource
.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Dans Swift3, voici comment vous définissez UITableViewDelegate
:
class FeedsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var feedsTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() feedsTableView.delegate = self feedsTableView.dataSource = self // Do any additional setup after loading the view. } }