2017-12-20 2 views
0

컬렉션보기가 있습니다. 컬렉션보기에는 8 개의 셀이 있습니다. 모든 셀에는 버튼이 있습니다. 버튼을 클릭하십시오. 이를 바닥에서보기를 추가하고 white.To에서 검은 색으로 그 텍스트 코르 변경해야, 다음 코드선택한 버튼의 배경 만 변경하고 다른 버튼의 배경은 버튼의 초기 상태와 동일하게 유지하십시오.

-(void)doSomething:(UIButton *) sender { 

    if(sender.isSelected){ 
    [sender setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal]; 
    recipeHeading = (RecipeHeadingCell*)[[sender superview] superview]; 
    NSIndexPath *path = [_headingCollectionView indexPathForCell:recipeHeading]; 
    UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, sender.frame.size.height + 2.0f, sender.frame.size.width, 30)]; 
    bottomBorder.backgroundColor = [UIColor whiteColor]; 
    [sender addSubview:bottomBorder]; 
    [_outerCollectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; 
     sender.selected=false; 
    } 
    else{ 
     NSLog(@"not selected"); 
     sender.selected=true; 
}  
} 

을 수행하지만이 버튼을 선택하면 예를 해결해야 할 또 하나 개의 시나리오가있다 셀 1에서 나머지 (이전에 선택된 것조차도)는 자동으로 선택 불가능한 상태가되어야합니다. 즉, 제목이 흰색이어야하며 맨 아래에보기가 없어야합니다. 내 코드는 선택 사항에서는 잘 작동하지만 선택되지 않은 다른 버튼의 상태는 변경되지 않습니다.이 방향에 대한 지침을 알려주십시오. 도움이나 제안을 보내 주시면 감사하겠습니다. 미리 감사드립니다!

답변

0
그것은 모델 추가 (불리언 배열을하고 cellForRowAtIndexpath에 액세스) 모든 선택 상태에 대한 true 또는 false 값을 가지는 셀 하나 개의 셀이 전체 테이블을 새로 고침을 클릭 할 때

을 다시 그려야 할 필요가

viewDidLoad에

@interface ViewController() 

{

NSMutableArray*allValues; 

}

allValues = [NSMutableArray new]; 

    for(int i = 0 ;i<YourTableViewArrayCount;i++) 
    { 
    [allValues addObject:[NSNumber numberWithBool:NO]]; 
    } 

하고있는 tableview 데이터 소스

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

    bool d = [allValues[indexPath.row] boolValue]; 

    if(d) 
    { 
     // cell selected 
    } 
    else 
    { 
     // cell not selected 
    } 

    /// implement remaining here 

} 

참고 : 글로벌 메이크업이 배열 및 또는 관찰자 NotificationCenter 으로 있는 tableView를 셀이있는 UITableViewCell 사용자 정의 클래스에서 클릭 할 때 값의 변경하고 다시로드

+0

감사합니다. 따라야 할 코드 스 니펫이나 링크를 제안 할 수 있습니까? – Prez

+0

업데이트를 참조하십시오. ... . . –

+0

감사합니다. 나를 위해 일했습니다. – Prez

0

컬렉션 뷰를 만들면 버튼 태그를 셀의 indexPath.item으로 제공하십시오. 버튼을 클릭하면 버튼의 태그를 전역 값으로 저장합니다. selectedRow을 호출하고 처음에는 값을 -1로 설정하고 콜렉션 뷰를 다시로드합니다. selectedRow 및 indexPath.item이 동일한 경우 인덱스의 항목에있는 셀에서 테두리를 표시하고 다른보기를 숨 깁니다.

희망 하시겠습니까?

관련 문제