이 코드를 사용하려면 : 여기
내 코드의
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSLog(@"row: %d",indexPath.row);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
// Add subviews like this:
// [[cell contentView] addSubview:objectName];
}
### Move this here ###
// And I get the row number like this: indexPath.row when getting objects from the array
return cell;
}
"나는 단지 8 개 세포를보고, 그들은 반복 반복합니다." 옳은.
당신이 놓치고 간 것이 그것이 어떻게 작동해야하는지입니다. 그래서 셀이 0 인 경우에만 새 셀을 초기화하는 &을 할당합니다. 그래서 당신은 할당하고 초기화하고 색상을 설정하고 if 문에 하위 뷰를 추가합니다. 그런 다음 if(cell==nil)
다음에 변수가 전달 된 일부 데이터로 채울 유효한 셀이 있음을 알고 있습니다.
이제는 셀을 설정하지 않고 표시된 모든 데이터를 할당하는 것이 문제입니다 indexPath
에 따라. 문제는 셀이 두 번째로 사용되지 않으므로 데이터가 변경되지 않는다는 것입니다.
속도 덧글에 더 자세히 설명하기 위해 이전 폴백 예를 사용합니다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *hugeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
hugeLabel.tag = 300;
[cell addSubview:hugeLabel];
}
[(UILabel *)[cell viewWithTag:300] setText:[arrayOfStrings objectAtIndex:indexPath.row]];
return cell;
}
위의 예제에서 보면, 우리가 그것을 설정 셀에 UILabel
를 추가 것을 볼 300 태그의 것 그런 후 문 우리는 새로운 세포 또는 재사용 세포 중 하나를 사용할 경우에 레이블에 텍스트가 이미 있습니다. 어떠한 방법 으로든 우리는 단순히 기존 레이블의 텍스트를 행을 고려해야하는 것으로 변경합니다. 이 방법으로 우리는 반복적 인 견해를 만드는 것을 피합니다.
는 캐시에 죽은 설정 당신이하는 경우 UITableViewCells
이 같은 할 수있는 :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < _cells.count){
return [_cells objectAtIndex:indexPath.row]; // _cells is an NSMutableArray setup in viewDidLoad
}
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
cell.textLabel.text = [source objectAtIndex:indexPath.row]; // source is an NSArray of NSStrings I set up in viewDidLoad
[_cells addObject:cell];
return cell;
}
주을 효율적 무엇 콘솔에서 당신이 Received memory warning
을 볼 때 장치에서이 놀라지 않는 실행하는 경우 & 쉬운 것은 종종 동일하지 않습니다.
알겠습니다. 감사합니다. 그것을 약간 수정하고 일하는 것 같습니다. 하지만 꽤 많은 하위 뷰를 추가하기 때문에로드 시간을 늘리는 방법에 대해 알고 있습니다. 저장 한 경우 하위 뷰를 저장하고 다시로드하는 것이 좋습니다. 생각? – iosfreak
NSLog를 셀 == nil에 추가하면 8 번만 나타납니다. 내가 아래로 스크롤했을 때조차도, 그것들을 반복하고 있습니다. – iosfreak
큐 해제 프로세스는 테이블 뷰 스크롤을 빠르게합니다. 네가 반복해서 반복 한 세포가 8 개 정도 있다고했다. 이러한 tableviewcell은 한 번만 생성되기 때문에 좋습니다.tableview의 dequeue 메소드는 새로운 tableviewcell을 만드는 대신 내용을 변경하고 다시 표시 할 수 있도록 화면 밖으로 스크롤 한 tableviewcell을 제공합니다. – NJones