iPhone에서 한 번의 클릭으로 두 개의 uibutton의 배경색을 변경하는 방법은 무엇입니까?

카르 틱

I 'm new to iPhone app development. In my app I 'm using tableview. in tableview I 'm using two uibuttons in white color and another one in graycolor . 회색 버튼을 클릭하면 흰색과 다른 버튼으로 변경됩니다. 버튼이 동시에 회색으로 변경되어야합니다.

이것은 내 코드입니다.

       -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    ExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell=nil;
    if(cell == nil)
     {
         cell = [[ExampleCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier];
     }

    if(indexPath.row == 0)
    {
        postbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [postbtn setFrame:CGRectMake(0, 205, 160, 34)];
        [postbtn setBackgroundColor:[UIColor whiteColor]];
        [postbtn setTitle:[NSString stringWithFormat:@"%@ POST",[AppDelegate.profileDic objectForKey:@"posts"]] forState:UIControlStateNormal];
        [postbtn.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:10]];
        [postbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [postbtn setTag:1001];
        [postbtn addTarget:self action:@selector(reloadAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:postbtn];

        likebtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [likebtn setFrame:CGRectMake(160, 205, 160, 34)];
        [likebtn setBackgroundColor:[UIColor lightGrayColor]];
        [likebtn setTitle:[NSString stringWithFormat:@"%@ LIKE",[AppDelegate.profileDic objectForKey:@"likes"]] forState:UIControlStateNormal];
        [likebtn.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:10]];
        [likebtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [likebtn setTag:1002];
        [likebtn addTarget:self action:@selector(reloadAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:likebtn];
        }
)};

    -(void)reloadAction:(id)sender
    {


        if ([sender tag] == 1001)
        {
           [postbtn setBackgroundColor:[UIColor whiteColor]];
           [likebtn setBackgroundColor:[UIColor lightGrayColor]];
            AppDelegate.selectedTab=1;
            [profiletableview reloadData];

        }
        if ([sender tag] == 1002)
        {
            [postbtn setBackgroundColor:[UIColor lightGrayColor]];
            [likebtn setBackgroundColor:[UIColor whiteColor]];
            AppDelegate.selectedTab=2;
            [self likewebservice];
            [profiletableview reloadData];
        }
    }

uibutton의 배경색을 변경하는 방법이 있습니까?

린지 스콧

문제는 다음을 포함하여 버튼을 누른 직후 테이블을 다시로드한다는 것입니다.

[profiletableview reloadData];

당신의 reloadAction:방법에. 이렇게하면에서 처음 정의 된 색상으로 색상을 재설정하는 것입니다 tableView:cellForRowAtIndexPath:. 그 라인을 제거하면 작동합니다.


편집하다:

그리고 테이블의 데이터를 다시로드 할 수 있어야하는 경우 tableView:cellForRowAtIndexPath:메서드 에서 UIButton을 초기화해서는 안됩니다 (또는 최소한 첫 번째로드 후에 UIButton을 반복해서 초기화하면 안됩니다). 당신은 당신이 이미 수행 한 것처럼 그들은, 다음, 테이블로드되기 전에 초기화하고, 그래서의 viewDidLoad 방법에 UIButtons를 초기화하는 동안 셀에 UIButtons를 추가하는 것이 가장 좋은 선택이 될 수 있습니다 tableView:cellForRowAtIndexPath:및시 버튼의 색상 값을 변경 reloadAction:. 내가 틀렸을 수도 있지만, reloadAction:이 경우에는 버튼을 직접 변경하고 동일한 방법으로 버튼 제목을 직접 변경할 수도 있으므로이 경우 테이블을 다시로드 할 필요가 없을 것입니다 . 그러나 일반적으로 버튼을 엉망으로 만들지 않고 테이블을 다시로드 할 수있는 옵션이있는 것이 좋습니다.

편집 # 2 :

또한 해당 행이 정적으로 유지되는 경우 reloadRowsAtIndexPaths : withRowAnimation을 사용하여 필요한 행만 다시로드하거나 KIDdAe가 말했듯이 전체 테이블 다시로드를 수행 할 수 있지만 cell == nil 인 경우에만 버튼을 셀에 추가 할 수 있습니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관