2011-09-20 22 views
0

所以我有2個按鈕用於每個小區,我已設法單獨地標記的表圖。我需要兩個按鈕被按下後才能禁用(投票功能)在哪裏我目前,按鈕在視圖中禁用,並且不會被點擊(儘管字體顏色甚至沒有響應,即使它設置爲UIControlStateDisabled,是另一個不太緊迫的問題)。但是,當我從禁用按鈕的視圖中滾動出來時,他們會再次點擊時觸發該操作。我怎樣才能確保保持這種狀態?我有這將分配的setEnabled基於陣列上的bool數組,但只有一半的作品,它幾乎一樣,如果我看到海誓山盟的最上面的扣子,但是,字體並在這一點上作出迴應。請大家幫忙。(和我很抱歉,不管別人怎麼說,這個網站確實需要重寫它的代碼粘貼的東西,整個4號線的事情是沒有效率可言)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

鑑於年齡我相信您現在可能已經對此進行了排序,但對於您或其他任何人來說,這與iOS緩存和重用表格視圖單元格的方式有關。

正如你在正確的代碼完成後,你已經使用

dequeueReusableCellWithIdentifier:CellIdentifier

這是偉大的表現,但是當它可以重複使用電池,它也將重用性。因此,在禁用按鈕的單元格中,當它們離開視圖時再回到視圖中時,iOS會重用其中一個未使用的單元格,這些單元格很可能沒有禁用按鈕,因此他們回來點擊。

在理論(這是我遇到的問題),你也應該得到被其他細胞禁用某些按鍵時,爲得到重用你的殘疾鈕釦電池變得不可見。

可以通過存儲哪些按鈕應該在索引被禁用修復它。如果您使用說你的表中的數據一個NSArray其中包含的NSDictionary對象,您可以添加兩個新按鍵本字典,每一個每單元2個的按鈕的狀態。在那裏你可以簡單地存儲一個布爾值(NSNumber)來表明它是否應該被禁用。然後在你的cellForRowAtIndexPath方法,添加以下

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

然後,每當按下按鈕,請務必相應地更新字典中的這些鍵。那麼你應該只對那些需要它:)

希望這有助於在所有細胞禁用按鈕。