How to simply populate TableView for iPhone

I have a UITableView. How easy is it to fill it with three elements, for example, "e1", "e2", "e3"?

+3
source share
3 answers

set the DataSource of your table in your class and define in class 3 methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *cellId = @"identifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellId];
  if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellId] autorelease];
  }
  [cell setText:[NSString stringWithFormat:@"e%i",indexPath:[indexPath row]];
  return cell;
}
+7
source

UITableView - , . UITableView . . . , , , . API , , .

Hellra1ser . , .

0

Code in swift 3.1:

Just in case, if someone needs to copy it for testing purposes.

extension ViewController : UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "Test"
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
}
0
source

Source: https://habr.com/ru/post/1702193/


All Articles