Как фильтровать табличное представление на основе значения выбора? У меня есть следующие NSMutableArray:Filter Tableview
[0]: Объект 1 (имя1, октябрь)
[1]: Объект 2 (имя2, ноябрь)
[2]: Объект 3 (name3, март)
[3]: Объект 4 (NAME4, апрель)
[4]: Объект 5 (name5, апрель)
вид таблицы отображает значение этих объектов. , и у меня есть сборщик с месячными значениями (январь, февраль, и т. Д.) , поэтому, основываясь на выборе пользователем значения выбора, в представлении таблицы должны отображаться значения, например, если я выбираю месяц апрель, только эти значения являются объектом 3 и 4.
код решения, благодаря Fennelouski: (просто думал, что я положил его здесь, в случае кто-то нуждается в ней)
NSMutableArray *array;
NSMutableArray *filteredArray;
int monthInt;
NSString *monthString;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Titles *object1 = [[Titles alloc]init];
[object1 setTitle:@"Title1"];
[object1 setMonth:@"10"];
Titles *object2 = [[Titles alloc]init];
object2.title = @"Title2";
object2.month = @"10";
Titles *object3 = [[Titles alloc]init];
object3.title = @"Title3";
object3.month = @"4";
array = [[NSMutableArray alloc]init];
filteredArray = [[NSMutableArray alloc]init];
[array addObject:object1];
[array addObject:object2];
[array addObject:object3];
pickerData = @[@"Jan", @"Feb", @"March", @"April", @"May", @"June", @"July", @"Aug", @"Sept", @"October", @"Nov", @"Dec"];
NSDate *currentDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
monthInt= [components month];
monthString = [NSString stringWithFormat:@"%li",(long)monthInt];
for (Titles *item in array)
{
if([item.month isEqualToString:monthString])
{
[filteredArray addObject:item];
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [filteredArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
Titles *titles = filteredArray[indexPath.row];
cell.textLabel.text = titles.title;
cell.imageView.image = [UIImage imageNamed:@"totoro.jpg"];
NSLog(@"Cell is %@", [array objectAtIndex:indexPath.row]);
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return pickerData.count;
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return pickerData[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"Row: %li", (long)row);
monthString = [NSString stringWithFormat:@"%li",row+1];
[filteredArray removeAllObjects];
for (Titles *item in array)
{
if([item.month isEqualToString:monthString])
{
[filteredArray addObject:item];
}
}
[self.tableview reloadData];
}
хотя бы упоминать причину, по которой я проголосовал, поэтому я могу улучшить свой вопрос ... – ElSS
, потому что ваш вопрос не показывает никаких усилий. Вам нужно показать, что вы пробовали, и что не так. –
Хорошо, я обновлю через несколько минут @ Mr.T – ElSS