UIButtons没有正确禁用

时间:2011-09-20 22:08:58

标签: uitableview uibutton

所以我有一个表格视图,每个单元格有2个按钮,我已经设法单独标记。我需要在按下一个按钮后禁用这两个按钮(投票功能)我当前在视图中按钮禁用,并且不会被点击(尽管字体颜色甚至没有响应它甚至是UIControlStateDisabled的设置,是另一个不太紧迫的问题)。但是,当我滚出禁用按钮的视图并返回时,它们将在单击时再次触发操作。我怎样才能确保保持这种状态?我有一个bool数组,它会根据数组分配setEnabled,但只有一半工作,它几乎就像我看到彼此顶部的按钮,但是,字体确实在这一点上做出响应。请帮助。(我很抱歉,尽管人们说,这个网站真的需要重写其代码粘贴的东西,整个4行的东西都没有效率)

// 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];

}

1 个答案:

答案 0 :(得分:0)

考虑到年龄,我确定你现在可能已经对它进行了排序,但是对于你或其他任何人来说,这与iOS缓存和重用表视图单元格的方式有关。

正如您在代码中所做的那样,您已经使用了

dequeueReusableCellWithIdentifier:CellIdentifier

这对性能很有帮助,但是当它重用一个单元格时,它也会重用这些属性。因此,在您已禁用按钮的单元格中,当它们离开视图,然后返回视图时,iOS会重复使用其中一个未使用的单元格,这很可能没有禁用其按钮,因此他们回来了。

理论上(就像我遇到的问题一样)当你的禁用按钮单元变得不可见时,你应该在其他单元格上禁用一些按钮,因为它会被重用。

您可以通过存储索引中应禁用的按钮来修复它。如果您使用NSArray表示包含NSDictionary对象的表数据,则可以向此字典添加2个新键,一个用于每个单元格的每个按钮的状态。在那里你可以简单地存储一个布尔值(NSNumber),说明是否应该禁用它。然后在您的cellForRowAtIndexpath方法中添加以下

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

然后,只要按下按钮,请确保相应地更新字典中的这些键。你应该只为需要它的单元格禁用按钮:)

希望这会有所帮助。