开发者

UIButtons not disabling correctly

开发者 https://www.devze.com 2023-04-06 01:39 出处:网络
So i have A table view with 2 buttons for each cell, which i have managed to individually tag. I need both buttons to be disabled after one is pressed (voting feature) Where i am currently, the button

So i have A table view with 2 buttons for each cell, which i have managed to individually tag. I need both buttons to be disabled after one is pressed (voting feature) Where i am currently, the buttons disable while in view, and wi开发者_运维技巧ll not be clicked(although the font color is not responding at all even tho its set for UIControlStateDisabled, which is another less pressing issue). However, when i scroll out of the view of the disabled buttons and come back they will fire the action again on clicked. How can i make sure to retain this state? I had an array of bools which would assign setEnabled based on the array but that only half works, its almost as if i see buttons on top of eachother, however, the font does respond at this point. please help.(and im sorry, despite what people say , this site REALLY needs to rewrite its code pasting stuff, the whole 4 line thing is not efficient at all)

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

}


Given the age I'm sure you've probably sorted this by now, but for you or anyone else, it's to do with the way iOS caches and reuses the table view cells.

As you've correctly done in the code, you've used

dequeueReusableCellWithIdentifier:CellIdentifier

This is great for performance, but when it reuses a cell, it'll also reuse the properties. So in the cell where you've disabled your buttons, when they go out of view, and then come back into view, iOS reuses one of the cells that isn't being used, which most likely DOESN'T have its buttons disabled, hence they come back clickable.

In theory (as is the problem I'm having) you should also get some buttons being disabled on other cells when your disabled button cell becomes invisible as that gets reused.

You can fix it by storing which buttons should be disabled in an index. If you're using say an NSArray for your tables data which contains NSDictionary objects, you could add 2 new keys to this dictionary, one for the state of each of your 2 buttons per cell. In that you could simply store a boolean (NSNumber) stating if it should be disabled or not. Then in your cellForRowAtIndexpath method, add the following

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

Then whenever the button is pressed, make sure you update these keys in the dictionary accordingly. You SHOULD then have buttons disabled only for the cells that need it :)

Hope this helps at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消