2010-05-14 3 views
2

배경이 거의없는 테이블 뷰는 테이블 뷰를 String으로 채우는 fetchedResultsController로 채워집니다. 이제 각 tableview 셀의 각 String 옆에 단추를 추가하려고합니다. 지금까지 configureCell : atIndexPath 메서드에서 단추를 만든 다음 해당 단추를 하위 셀로 표 셀의 내용보기에 추가하려고했지만 어떤 이유로 단추가 표시되지 않습니다. 아래는 관련 코드입니다. 더 많은 코드를 게시하려면 원하는대로 물어보십시오. 어떤 도움이라도 대단히 감사합니다.iPhone 질문 : 어떻게하면 테이블 뷰 셀에 버튼을 추가 할 수 있습니까?

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 

// get object that has the string that will be put in the table cell 
Task *task = [fetchedResultsController objectAtIndexPath:indexPath]; 

//make button 
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
[button setTitle:@"Check" forState:UIControlStateNormal]; 
[button setTitle:@"Checked" forState:UIControlStateHighlighted]; 

//set the table cell text equal to the object's property 
cell.textLabel.text = [task taskName]; 

//addbutton as a subview 
[cell.contentView addSubview:button]; 
} 

- (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] autorelease]; 
    } 

// Configure the cell. 
[self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 

답변

2

세 코멘트 :

  1. 당신은 아마 당신의 UIButton의 프레임을 설정해야합니다. 그렇지 않으면 얼마나 큰지, 어디에 배치해야 하는지를 알지 못합니다. 기본 프레임이있을 수도 있지만 조사하지는 않았습니다.
  2. 버튼을 누출 중입니다. 전화 후에 을 호출 할 필요가 없습니다. [UIButton buttonWithType:...];
  3. 동일한 셀에 여러 단추를 추가 할 가능성이 있습니다. 셀을 재사용하는 경우 먼저 removeFromSuperview 서브 뷰 (cell.contentView)를 호출해야합니다.
+0

답변을 주셔서 감사합니다. [UIButton buttonWithType]을 으로 변경했습니다. CGRect buttonFrame = CGRectMake (0, 0, 40, 40); UIButton * button = [[UIButton alloc] initWithFrame : buttonFrame]; 아무 것도 나타나지 않습니다. 그것이 뭔가에 의해 가려진 것 같습니까? 편집 : 다른 문자열 텍스트에서 일부 텍스트를 볼 수 있습니다. 이제 나는 그것을 옮길 필요가있다. 당신이 도와 줘서 고마워. – Jake

+0

'+ buttonWithType :'은'UIButtons'을위한 적절한 이니셜 라이저입니다. 다음과 같이하면됩니다 :'[button setFrame : CGRectMake (0,0,40,40)];' –

관련 문제