문제

나를 디자인할 수 있 사용자 지정 UITableViewCells 와 부하들을 잘 설명하는 기술을 사용하에서 스레드에서 발견 http://forums.macrumors.com/showthread.php?t=545061.그러나 그 방법을 사용하여 더 이상 할 수 있습 init 셀해서 identifier 에 의미를 만들어야 전체 새 인스턴스를 각각의 세포에서 모든 call.사람이 생각하는 좋은 방법이 여전히 캐시 특정한 세포 형태에 대한 다시 사용하지만,여전히 될 수 있는 디자인에서 그들을 인터페이스 Builder?

도움이 되었습니까?

해결책

적절한 메소드 서명으로 메소드를 구현하기 만하면됩니다.

- (NSString *) reuseIdentifier {
  return @"myIdentifier";
}

다른 팁

실제로 인터페이스 빌더에서 셀을 구축하고 있으므로 재사용 식별자를 설정합니다.

IB_reuse_identifier

또는 Xcode 4를 실행중인 경우 Attributes Inspector 탭을 확인하십시오.

enter image description here

(편집 : xcode에 의해 Xib가 생성 된 후에는 빈 uiview가 포함되어 있지만 uitableviewcell이 필요합니다. 따라서 uiview를 수동으로 제거하고 테이블 뷰 셀을 삽입해야합니다. 물론, IB는 uitableviewcell 매개 변수를 표시하지 않습니다. uiview.)

이제 iOS 5에는 그에 대한 적절한 uabitteview 방법이 있습니다.

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier

이 코드를 원래 어디에서 찾았는지 기억할 수는 없지만 지금까지 저에게는 잘 작동했습니다.

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomTableCell";
    static NSString *CellNib = @"CustomTableCellView";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }

    // perform additional custom work...

    return cell;
}

예제 인터페이스 빌더 설정 ...

alt text

답했 이 질문:

그것은 가능한 디자인 NSCell 서브 클래스에서는 인터페이스 Builder?

그것은 단지 가능한 디자인 UITableViewCell 에서는 IB,그것은 바람직하기 때문에 그렇지 않으면 모든 설명서의 배선 및 배치의 여러 요소가 매우 지루한 수 있습니다.Performaance 는 만큼 당신은 신중하게 모든 요소가 불투명 가능한 경우.이 reuseID 에 IB 의 속성에 대한 UITableViewCell,다음 사용하신 일치하는 재사용 ID 코드에서 하려고 할 때에서 제외.

도에서 일부 발표자에서 곧 작년에는 당신이 안 만들 테이블 보기세포에서는 IB,하지만 그의 부하 침대입니다.

iOS 4.0에 따라 iOS 문서에는이 작업을 매우 빠르게 만드는 특정 지침이 있습니다.

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/tableviewcells/tableviewcells.html#//apple_ref/doc/uid/tp40007451-ch7

uitableviewcell 서브 클래스에 대한 설명 위치로 스크롤하십시오.

다음은 또 다른 옵션입니다.

NSString * cellId = @"reuseCell";  
//...
NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];

for (id obj in nibObjects)
{
    if ([obj isKindOfClass:[CustomTableCell class]])
    {
        cell = obj;
        [cell setValue:cellId forKey:@"reuseIdentifier"];
        break;
    }
}

Iboutlet을 통해 셀을 연결하는 것을 제외하고는 비슷한 방식으로 사용자 정의보기 셀을 만듭니다.

그만큼 [nib objectAt...] 접근 방식은 배열의 항목 위치 변경에 취약합니다.

그만큼 UIViewController 접근 방식은 좋습니다. 그냥 시도해 보았고 충분히 잘 작동합니다.

하지만...

모든 경우에 initWithStyle 생성자가 호출되지 않으므로 기본 초기화가 수행되지 않습니다.

사용에 대한 다양한 장소를 읽었습니다 initWithCoder 또는 awakeFromNib, 그러나 이것들 중 하나가 올바른 방법이라는 결정적인 증거는 없습니다.

명시 적으로 초기화 방법을 호출하는 것 외에도 cellForRowAtIndexPath 방법 아직 답변을 찾지 못했습니다.

얼마 전 나는이 주제에 대한 훌륭한 블로그 게시물을 찾았습니다. blog.atebits.com, 그 후 Loren Brichter AbtableViewCell 클래스를 사용하여 모든 uitableviewcells를 수행하기 시작했습니다.

당신은 모든 위젯을 넣을 간단한 컨테이너 Uiview로 끝나고 스크롤은 번개가 빠릅니다.

이것이 유용하기를 바랍니다.

이 기술은 또한 메모리 관리를 위해 View Controller에 펑키 IVAR이 필요하지 않습니다. 여기에서 사용자 정의 테이블보기 셀은 "CustomCell.xib"라는 XIB에 있습니다.

 static NSData *sLoadedCustomCell = nil;

 cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
 if (cell == nil) 
 {
   if (sLoadedCustomCell == nil) 
   {        
      // Load the custom table cell xib
      // and extract a reference to the cell object returned
      // and cache it in a static to avoid reloading the nib again.

      for (id loadedObject in [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]) 
      {
        if ([loadedObject isKindOfClass:[UITableViewCell class]]) 
        {
          sLoadedCustomCell = [[NSKeyedArchiver archivedDataWithRootObject: loadedObject] retain];
          break;
        }
    }
    cell = (UITableViewCell *)[NSKeyedUnarchiver unarchiveObjectWithData: sLoadedCustomCell];
  }

루이스 방법은 나를 위해 일했습니다. 이것은 펜촉에서 uitableviewcell을 만드는 데 사용하는 코드입니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellId"];

    if (cell == nil) 
    {
        UIViewController *c = [[UIViewController alloc] initWithNibName:@"CustomCell" bundle:nil];
        cell = (PostCell *)c.view;
        [c release];
    }

    return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *simpleTableIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}         

return cell;
}

GustaVogB 솔루션은 나에게 효과가 없습니다. 내가 시도한 것은 다음과 같습니다.

ChainesController *c = [[ChainesController alloc] initWithNibName:@"ChainesController" bundle:nil];
[[NSBundle mainBundle] loadNibNamed:@"ChaineArticleCell" owner:c options:nil];
cell = [c.blogTableViewCell retain];
[c release];

작동하는 것 같습니다. BlogtableViewCell은 셀의 iBoutlet이며 ChainEscontroller는 파일의 소유자입니다.

Uitable view 문서에서 dequeueWithReuseIdentifier: "재사용 할 셀 객체를 식별하는 문자열. 기본적으로 재사용 가능한 셀의 식별자는 클래스 이름이지만 임의의 값으로 변경할 수 있습니다."

재정의 -리시 덴티퍼 자신은 위험합니다. 셀 서브 클래스의 두 개의 서브 클래스가 있고 단일 테이블보기에서 두 가지를 사용하면 어떻게됩니까? 그들이 재사용 식별자 호출을 Super로 보내면 잘못된 유형의 셀을 dequeue 할 것입니다 .............. 재사용 식별기 방법을 무시해야한다고 생각하지만 대체 식별자를 반환해야합니다. 끈. 또는 지정되지 않은 경우 클래스를 문자열로 반환하도록하십시오.

그 가치가 무엇인지, 나는 iPhone Tech Talks 중 하나에서 iPhone 엔지니어에게 이것에 대해 물었습니다. 그의 대답은 "그렇습니다. IB를 사용하여 셀을 만들 수 있습니다. 그러나하지 마십시오. 제발하지 마십시오."

나는 Ben Mosher가 연결 한 Apple의 지시를 따랐지만 Apple은 중요한 요점을 생략했다는 것을 알았습니다. 그들이 IB에서 디자인하는 객체는 단지 UitableViewCell 일뿐입니다. 그러나 실제로 UitableViewCell의 사용자 정의 서브 클래스로 설정하고 서브 클래스에 대한 코드 파일을 작성하면 코드에 iBoutlet 선언 및 iBaction 메소드를 작성하여 IB의 사용자 정의 요소에 연결할 수 있습니다. 그런 다음 이러한 요소에 액세스하기 위해 뷰 태그를 사용할 필요가 없으며 원하는 미친 셀을 만들 수 있습니다. 코코아 터치 천국입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top