iOS 設定Button的 初始選中狀態
btnMutableArray = [[NSMutableArray alloc]init]; //將button放到數組裡面
NSMutableDictionary *titleDic = [[NSMutableDictionary alloc]init]; //這個是二級標題的所有資訊的字典 首頁返回的
for (int i = 0; i < titleMutableArray.count; i++) {
titleDic = [titleMutableArray objectAtIndex:i];
allButton = [[UIButton alloc]
[allButton setTitle:titleDic[@"name"] forState:UIControlStateNormal];
classid = [titleDic[@"id"]intValue];
allButton.tag = classid;
allButton.titleLabel.font = [UIFont systemFontOfSize:14];
[allButton setTitleColor:
[allButton setTitleColor:RGBCOLOR(51, 51, 51) forState:UIControlStateNormal];
allButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[allButton addTarget:self action:@selector(fashionButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[headerScrollerView addSubview:allButton];
[btnMutableArray addObject:allButton];
}
((UIButton *)[btnMutableArray objectAtIndex:0]).selected=YES; // 關鍵是這裡,設定 陣列的第一個button為選中狀態
//點選事件
-(void)fashionButtonPressed:(UIButton *)sender{
[myFashionMutableArray removeAllObjects];
[_myfashionTableView reloadData];
((UIButton *)[btnMutableArray objectAtIndex:0]).selected=NO; //點選其他button之後這裡設定為非選中狀態,否則會出現2個紅色的選中狀態
if (sender != allButton) {
allButton.selected = NO;
allButton = sender;
} //這個if是網上抄的,有點神奇
allButton.selected = YES;
_fashionpid = sender.tag;
pageSize = 0;
[self loaddataMethodsWithpid:_fashionpid page:pageSize number:Baby_NumofList andSord:0];
}