2014-04-04 4 views
1

일부 버튼을 요소로 사용하여 테이블보기를 만들면 뷰가 잘로드되지만 아래로 스크롤하는 단추는 겹쳐서 표시됩니다. 나는 오래된 버튼이 지워지지 않고 새로운 버튼이 그 위에 위치한다고 생각한다. 이 문제를 해결하도록 도와주세요. 미리 감사드립니다.스크롤하는 동안 UIButton이있는 UITableViewCells가 있습니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath   (NSIndexPath *)indexPath 
{ 
NSLog(@"index path %ld",(long)indexPath.row); 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if(cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
} 


    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal]; 
    button.frame = CGRectMake(0, 0, 160.0, 25); 
    [cell.contentView addSubview:button]; 


return cell; 
} 
+0

당신은 삭제할 수 있습니다 사용할 수있는 (셀 == 전무) 코드 블록이 dequeueReusableCellWithIdentifier 이미 충분합니다. – etolstoy

답변

2

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] 

당신이 하위 뷰 같은 버튼을 추가하기 직전에이 모든 버튼을 제거, 코드 아래에 삽입하십시오 지금

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

    (or) //use this in cell for rowatindexpath 

for(UIView *view in cell.contentView.subviews){ 
    if ([view isKindOfClass:[UIView class]]) { 
     [view removeFromSuperview]; 
    } 
} 
+0

무슨 일이 있었는지, 그 작품은 괜찮거나하지 –

+0

네 너무 잘 작동 .. 감사합니다 –

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath   (NSIndexPath *)indexPath 
{ 
NSLog(@"index path %ld",(long)indexPath.row); 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

//if(cell == nil) 
//{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
//} 


UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal]; 
button.frame = CGRectMake(0, 0, 160.0, 25); 
[cell.contentView addSubview:button]; 


return cell; 
} 
0

인 라인을 대체하여 새 셀을 추가하기 전에 셀의 내용보기

for(UIView *view in cell.contentView.subviews){ 
    if ([view isKindOfClass:[UIButton class]]) { 
     [view removeFromSuperview]; 
    } 
} 
+0

감사 .... 그것은 나를 위해 잘 작동 .. Logged –

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"index path %ld",(long)indexPath.row); 

    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell = nil; 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 



     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal]; 
     button.frame = CGRectMake(0, 0, 160.0, 25); 
     [cell.contentView addSubview:button]; 
    } 

    return cell; 
} 
1

나는이 태그를 사용하는 것이 좋습니다 것입니다. 아래 코드를 참조하십시오. 당신이 XIB를 사용하는 경우

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

    NSLog(@"index path %ld",(long)indexPath.row); 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    } 

    NSInteger buttonTag = 100500; 
    UIButton *button = [cell.contentView viewWithTag: buttonTag] 
    if(button == nil) 
    { 
     button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = CGRectMake(0, 0, 160.0, 25); 
     [cell.contentView addSubview:button]; 
    } 
    [button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal]; 

    return cell; 

} 
1

당신이

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

if (cell==nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
관련 문제