標籤:led def seq inf tab setvalue name class
實現思路比較簡單,這裡僅做記錄:
直接上代碼:
1,實現didSelectRowAtIndexPath方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [[NSUserDefaults standardUserDefaults]setValue:[array objectAtIndex:indexPath.row] forKey:APP_CHANGEVOICE]; [_sextTableView reloadData]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
在cellForRowAtIndexPath裡面實現方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellTableIdentifier = @"CellTableIdentifier"; hPublickCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell == nil) { cell = [[hPublickCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellTableIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } cell.textsLabel.text = array[indexPath.row]; cell.selectionStyle=UITableViewCellSelectionStyleGray; //選擇狀態的儲存 if ([[[NSUserDefaults standardUserDefaults]valueForKey:APP_CHANGEVOICE] isEqualToString:[array objectAtIndex:indexPath.row]]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; }
這裡面的array是資料來源數組。如下:
2,上面這種是系統的選中樣式,下面是自訂的:
代碼如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ShippingAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShippingAddressCell"]; if (!cell) { cell = [[ShippingAddressCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ShippingAddressCell"]; } if (self.lastIndexPath == indexPath) { cell.selectedImg.image = [UIImage imageNamed:@"clicked"]; }else { cell.selectedImg.image = [UIImage imageNamed:@"unClick"]; } return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //之前選中的,取消選擇 ShippingAddressCell *celled = [tableView cellForRowAtIndexPath:_lastIndexPath]; celled.selectedImg.image = [UIImage imageNamed:@"unClick"]; //記錄當前選中的位置索引 _lastIndexPath = indexPath; //當前選擇的打勾 ShippingAddressCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.selectedImg.image = [UIImage imageNamed:@"clicked"];}
這樣就可以實現了,如下:
多選的有空再完善!代碼可以直接粘貼使用!
ios開發之--tableview單選實現