Jak działa cellForRowAtIndexPath?
Przeczytałem dokumentację apple i nie jest to zrozumiałe dla tak początkującego w Objective-C
Jak ja. Staram się zaimplementować multicolumn UITableView
podążając za tym link przykład i to po prostu nie działa, więc muszę zrozumieć, jak działa cellForRowAtIndexPath
, ponieważ dla mnie osobiście ta metoda wydaje się dość skomplikowana.
1) co zwraca? UITableViewCell
? Ale dlaczego to wygląda tak dziwnie?
-(UITableViewCell *)tableView:(UITableView *)tableView
- Co to jest? Możesz to wyjaśnić?
2) Jak to się wywołane i co jest ważniejsze jak mam go połączyć z pewnym UITableView
??? Co zrobić, jeśli mam dwa UITableView
'S o nazwie firstTableView
i secondTableView
i chcę, aby były inne (aby wykonać cellForRowAtIndexPath
inaczej)? Jak mam połączyć moje UITableViews
z tym
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Metoda przyjmuje NSIndexPath
, a nie UITableView
. Co ja teraz zrobię?
3 answers
1) funkcja zwraca komórkę dla widoku tabeli tak? Zwracany obiekt jest więc typu UITableViewCell
. Są to obiekty widoczne w wierszach tabeli. Ta funkcja w zasadzie zwraca komórkę dla widoku tabeli.
Ale możesz zapytać, skąd funkcja będzie wiedziała, która komórka zwróci dla jakiego wiersza, co jest odpowiedzią w 2. pytaniu
2)NSIndexPath
jest zasadniczo dwie rzeczy -
- Twoja Sekcja
- twój wiersz
Ponieważ twój stół może być podzielony na wiele sekcje i każdy z własnymi wierszami, to NSIndexPath
pomoże Ci dokładnie określić, która sekcja I który wiersz. Obie są liczbami całkowitymi. Jeśli jesteś początkującym, powiedziałbym, że spróbuj z jedną sekcją.
Jest wywoływany, jeśli zaimplementujesz protokół UITableViewDataSource
w kontrolerze widoku. Prostszym sposobem byłoby dodanie klasy UITableViewController
. Zdecydowanie polecam, ponieważ Apple ma trochę kodu napisanego dla ciebie, aby łatwo zaimplementować funkcje, które mogą opisywać tabelę. W każdym razie, jeśli zdecydujesz się wdrożyć ten protokół należy utworzyć obiekt UITableViewCell
i zwrócić go dla dowolnego wiersza. Spójrz na jego odniesienie do klasy, aby zrozumieć ponowną użyteczność, ponieważ komórki, które są wyświetlane w widoku tabeli, są ponownie używane ponownie i ponownie (jest to bardzo wydajna konstrukcja btw).
Jeśli chodzi o dwa widoki tabeli, spójrz na metodę. Widok tabeli jest do niego przekazywany, więc nie powinieneś mieć z tym problemu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-07 09:55:15
Spróbuję to rozbić (przykład z } )
/*
* The cellForRowAtIndexPath takes for argument the tableView (so if the same object
* is delegate for several tableViews it can identify which one is asking for a cell),
* and an indexPath which determines which row and section the cell is returned for.
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/*
* This is an important bit, it asks the table view if it has any available cells
* already created which it is not using (if they are offScreen), so that it can
* reuse them (saving the time of alloc/init/load from xib a new cell ).
* The identifier is there to differentiate between different types of cells
* (you can display different types of cells in the same table view)
*/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
/*
* If the cell is nil it means no cell was available for reuse and that we should
* create a new one.
*/
if (cell == nil) {
/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
/*
* Now that we have a cell we can configure it to display the data corresponding to
* this row/section
*/
NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"mainTitleKey"];
cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"];
NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
/* Now that the cell is configured we return it to the table view so that it can display it */
return cell;
}
Jest to metoda DataSource
, więc zostanie wywołana na obiekcie, który zadeklarował się jako DataSource
z UITableView
. Jest wywoływana, gdy widok tabeli rzeczywiście musi wyświetlić komórkę na ekranie, w oparciu o liczbę wierszy i sekcji (które można określić w innych metodach DataSource).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-05 10:07:44
Zasadniczo projektuje twoją komórkę, cellforrowatindexpath jest wywoływany dla każdej komórki, a numer komórki jest znaleziony przez indexpath.wiersz i numer sekcji według ścieżki indeksowej.sekcja . Tutaj możesz użyć etykiety, przycisku lub obrazka tekstowego, które są aktualizowane dla wszystkich wierszy w tabeli. Odpowiedź na drugie pytanie W komórce dla wiersza na ścieżce indeksu użyj instrukcji if
W Celu C
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(tableView == firstTableView)
{
//code for first table view
[cell.contentView addSubview: someView];
}
if(tableview == secondTableView)
{
//code for secondTableView
[cell.contentView addSubview: someView];
}
return cell;
}
W Swift 3.0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
if(tableView == firstTableView) {
//code for first table view
}
if(tableview == secondTableView) {
//code for secondTableView
}
return cell
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-25 09:48:05