多线程中,NSTimer经常用到了,其中的userInfo属性大部分人都是直接赋nil,没去管它起啥作用。其实它是有作用的…传递信息。例子如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *cellLabel = (UILabel *)[newCell.contentView viewWithTag:1];
[newCell setSelected:YES animated:YES];

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
[myDictionary setObject:tableView forKey:@"table"];
[myDictionary setObject:indexPath forKey:@"indexPath"];
// The colon after the onTimer allows for the argument
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer:) userInfo:myDictionary repeats:NO];
[myDictionary release];
}
半秒钟触发一次onTimer: ,userInfo是以NSDictionary的方式传递信息给onTimer:
- (void)onTimer:(NSTimer *)timer {
NSLog(@"--- %@", [timer userInfo] );
[[[timer userInfo] objectForKey:@"table"] deselectRowAtIndexPath:[[timer userInfo] objectForKey:@"indexPath"] animated:YES];
// I have a reference to the tableView so I can do this below
// but to show how the keys work, the call above these works
//[table deselectRowAtIndexPath:[[timer userInfo] objectForKey:@"indexPath"] animated:YES];
}
你地明白?