2011-09-20 4 views
0

그래서 개별적으로 태그를 관리 할 수있는 각 셀에 2 개의 버튼이있는 테이블보기가 있습니다. 나는 하나의 버튼을 눌렀을 때 (UIControlStateDisabled에 대해 설정 한 글꼴 색상이 모두 응답하지 않더라도) 버튼을 사용하지 않도록 설정해야합니다 (투표 기능). 나는 현재 버튼이 보이지 않는 동안 버튼을 사용하지 않고 클릭하지 않습니다. 또 다른 덜 중요한 문제입니다). 그러나 비활성화 된 버튼보기에서 스크롤하여 돌아 왔을 때 클릭 한 상태에서 다시 작업을 시작합니다. 이 상태를 유지하려면 어떻게해야합니까? 배열을 기반으로 setEnabled를 할당하는 bool 배열을 가졌지 만 절반 만 작동합니다. 즉, 서로의 상단에있는 단추를 보는 것처럼 글꼴은이 시점에서 응답합니다. . 도와주세요 나이 감안할 때UIButtons가 올바르게 비활성화되지 않습니다.

// Customize the appearance of table view cells. 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
     (NSIndexPath*)indexPath { 


static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

NSDictionary *aTrip = [trips objectAtIndex:[indexPath row]]; 

cell.textLabel.text =[NSString stringWithFormat:@"%@& *** posted by %@ %@",[aTrip 
    objectForKey:@"txt"], 
         [aTrip objectForKey:@"name"], [aTrip  
    objectForKey:@"time"]]; 
cell.textLabel.adjustsFontSizeToFitWidth = YES; 
cell.textLabel.font = [UIFont systemFontOfSize:12]; 
cell.textLabel.minimumFontSize = 10; 
cell.textLabel.numberOfLines = 4; 
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
cell.detailTextLabel.text = [aTrip objectForKey:@"name"]; 

UIButton *upvote =[UIButton buttonWithType:UIButtonTypeCustom]; 

UIImage *upVoteBack = [UIImage imageNamed:@"arrowup.png"]; 
upvote.tag = 2*[indexPath row]; 
NSLog(@"the tag for upbutton is %d",upvote.tag); 
[upvote setBackgroundImage:upVoteBack forState:UIControlStateNormal]; 
upvote.titleLabel.font = [UIFont boldSystemFontOfSize:12.0]; 
[upvote setTitleColor:[UIColor redColor] forState:UIControlStateDisabled]; 
[upvote setTitle:[buttonTitles objectAtIndex:[indexPath row]] forState:UIControlStateNormal]; 
upvote.frame = CGRectMake(250.0f, 40.0f, 25.0f, 25.0f); 
[upvote addTarget:self 
     action:@selector(upvoteaction:)forControlEvents:UIControlEventTouchUpInside]; 
[upvote retain]; 
[cell addSubview:upvote]; 


UIButton *downvote =[UIButton buttonWithType:UIButtonTypeCustom]; 

UIImage *downVoteBack = [UIImage imageNamed:@"arrowdown.png"]; 
downvote.titleLabel.font = [UIFont boldSystemFontOfSize:12.0]; 
[downvote setTitle:[buttonTitles2 objectAtIndex:[indexPath row]]  
    forState:UIControlStateNormal]; 
downvote.tag = 2*[indexPath row]+1; 
NSLog(@"the tag for downvote is %d",downvote.tag); 
[downvote setBackgroundImage:downVoteBack forState:UIControlStateNormal]; 
downvote.frame = CGRectMake(280.0f, 40.0f, 25.0f, 25.0f); 
[downvote addTarget:self 
     action:@selector(upvoteaction:)forControlEvents:UIControlEventTouchUpInside]; 
[cell addSubview:downvote]; 


cell.selectionStyle = UITableViewCellSelectionStyleNone; 


return cell; 

} 


-(void) upvoteaction:(id) sender{ 
NSString *upnumber = @"12"; 
NSString *downnumber = @"5"; 
NSString *upordown; 
int k; 

if([sender tag]%2){ 
    upordown = @"2"; 
    k=-1; 
} 
else{ 
    k=1; 
    upordown = @"1"; 
} 
NSLog(@"upordown is %@", upordown); 

NSLog(@"sender tag is %d",[sender tag]); 

NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[sender superview]]; 



NSDictionary *trip = [trips objectAtIndex:[indexPath row]]; 

NSString *IDtoUse = [trip objectForKey:@"id"]; 
IDhold = IDtoUse; 

NSString *post =[NSString stringWithFormat:@"id=%@&vote=%@",IDhold, upordown]; 

NSLog(@"IDhold is %@", IDhold); 

NSNumber *wrapped = [NSNumber numberWithBool:NO]; 

UIButton *button1 = (UIButton *)[[sender superview] viewWithTag:[sender tag]]; 
button1.enabled = NO; 

UIButton *button2 = (UIButton *)[[sender superview] viewWithTag:[sender tag]+k]; 
button2.enabled = NO; 
[buttonTitles replaceObjectAtIndex:[indexPath row] withObject:upnumber]; 
[buttonTitles2 replaceObjectAtIndex:[indexPath row] withObject:downnumber]; 

/* 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:@"http://livepartyscene.com/tfln.php?"]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSError *error; 
NSURLResponse *response; 
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
*/ 

[self.tableView reloadData]; 
[IDhold release]; 

}

답변

0

(사람들이 무슨 말을하고 있으며,이 사이트 정말 그 코드를 붙여 물건을 다시 작성할 필요에도 불구하고, 미안 메신저, 전체 4 선 것은 전혀 효율적이지 않다) 아마 당신이 지금까지 이것을 정렬 했겠지만, 당신이나 다른 누구에게나 iOS가 캐시하고 테이블 뷰 셀을 재사용하는 것과 관련이 있습니다.

가 올바르게 코드에서 수행 한, 당신은

dequeueReusableCellWithIdentifier:CellIdentifier

이 성능에 좋은 곳입니다 사용했지만, 그 셀을 재사용 할 때, 그것은 또한 속성을 다시 사용할 수 있습니다. 따라서 버튼을 비활성화 한 셀에서보기를 벗어난 상태로 돌아 오면 iOS는 사용되지 않는 셀 중 하나를 재사용합니다. 대부분 버튼이 비활성화되어 있지 않으므로, 그러므로 그들은 다시 클릭 가능합니다.

이론적으로 (내가 가지고있는 문제 인 것처럼) 비활성화 된 단추 셀이 다시 사용되면 보이지 않게되면 다른 셀에서 일부 단추가 비활성화되어야합니다.

인덱스에서 비활성화해야하는 버튼을 저장하여 수정할 수 있습니다. NSDictionary 객체가 포함 된 테이블 데이터에 대해 NSArray라고 말하면 두 개의 새 키를이 사전에 추가 할 수 있습니다. 하나는 셀당 2 개의 버튼 상태를 나타냅니다. 당신이 단순히 불리언 (NSNumber)을 저장해야만 사용할 수 있는지 여부를 나타낼 수 있습니다. 그런 다음 cellForRowAtIndexpath 방법으로, 버튼을 누를 때마다, 그런 다음

upVote.enabled = [aTrip objectForKey:@"upVoteEnabled"].boolValue; 
downVote.enabled = [aTrip objectForKey:@"downVoteEnabled"].boolValue; 

를 추가하면 그에 따라 사전에 이러한 키를 업데이트해야합니다. 당신은 그때 그것을 필요로하는 세포에 대해서만 버튼을 비활성화해야합니다 :)

희망이 전혀 도움이됩니다.

관련 문제