Как я могу удалить значения ячеек таблицы в цели c? - PullRequest
0 голосов
/ 10 февраля 2012

Я пробую этот код, но он вылетает в - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) линия.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell" ;

Custom *cell = (Custom *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;

if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"Custom" owner:self options:nil];
    cell = satir;
}


// Configure the cell.
cell.adLabel.text=[array objectAtIndex:[indexPath row]];
cell.adLabel.textColor=   [UIColor darkGrayColor];
cell.adLabel.font = [UIFont systemFontOfSize:15];

cell.adresTextView.text=[array2 objectAtIndex:[indexPath row]];
cell.adresTextView.textColor=   [UIColor darkGrayColor];
cell.adresTextView.font = [UIFont systemFontOfSize:13];

cell.telefonLabel.text=[array3 objectAtIndex:[indexPath row]];
cell.telefonLabel.textColor=   [UIColor darkGrayColor];
cell.telefonLabel.font = [UIFont systemFontOfSize:13];


return cell;


}

- (IBAction)tablodaGosterBtnClick:(id)sender {

NSUInteger numComponents = [[pickerView dataSource] numberOfComponentsInPickerView:pickerView];

NSMutableString * text = [NSMutableString string];
for(NSUInteger i = 0; i < numComponents; ++i) {
    selectedRow = [pickerView selectedRowInComponent:i];
    title = [[pickerView delegate] pickerView:pickerView titleForRow:selectedRow forComponent:i];
    [text appendFormat:@"Selected item \"%@\" in component %lu\n", title, i];
}
if ([title isEqualToString:@"Restaurant"]) {

    array = [[NSMutableArray alloc] initWithObjects:@"Ehl-i Keyf Cafe", @"Enginarcı Mini Hotel ",@"Ginza Cafe",@"Lins Cafe",@"Opera Patisserie & Cafe",@"Patsa Cafe ",@"Sita Balık Evi ",nil];

    array2 = [[NSMutableArray alloc] initWithObjects:@"Fulya Mah. Mevlüt Pehlivan Cad. Erdoğanlar İş Merkezi No:14 Şişli/İstanbul",@"Fulya Mah. Mevlüt Pehlivan Sok. No:6 Fulya/İstanbul",@"Fulya Mah. Mevlüt Pehlivan Sok. No:13/A Şişli/İstanbul",@"Fulya Mah. Mevlüt Pehlivan Sok. No:13/A Şişli/İstanbul",@"Mevlüt Pehlivan Cad. No:25/2 Şişli/İstanbul",@"Fulya Mah. Mevlüt Pehlivan Sok. Multinet Plaza No:12 Şişli/İstanbul",@"Mevlüt Pehlivan Sok. No:9 Şişli/İstanbul", nil];

    array3 = [[NSMutableArray alloc] initWithObjects:@"0212 272 37 06",@"0536 304 33 28",@"0212 275 60 36",@"0212 204 15 15",@"0212 288 48 58",@"0212 336 88 00",@"0212 267 16 88", nil];

    [table reloadData];
}
else {

    [array removeAllObjects];
    [array2 removeAllObjects];
    [array3 removeAllObjects];
    [table reloadData];
}


}

Как я могу решить это? Возможно ли, исчезнуть табличное представление данных или удалить?

Ответы [ 3 ]

0 голосов
/ 10 февраля 2012
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

вызывается до того, как открывается вид, поэтому у вас не будет возможности нажать кнопку и позвонить

- (IBAction)tablodaGosterBtnClick:(id)sender

Следовательно, вы должны переместить инициализацию массива куда-нибудь еще. Если вы используете массив в качестве источника данных для табличного представления, тогда вам лучше всего обратиться к

-(void)ViewDidLoad
0 голосов
/ 18 апреля 2018
As per your comment

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 7;    //You need to replace your code here. You have to return whatever value in array and it must be same value count in all array which you are using.
}

if you return number of 7 and you are remove all value form array in 

- (IBAction)tablodaGosterBtnClick:(id)sender {

 //Remove value from array
}

when you reload tableview 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

//Your code

cell.adLabel.text=[array objectAtIndex:[indexPath row]]; //Here Array is empty and you are try to get value from it so its crashing here.

//Your code
}
0 голосов
/ 10 февраля 2012

Другой вариант - не удалять элемент из источника данных, а изменять высоту строки, чтобы динамически скрывать его.Вы можете сделать это, используя - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath метод делегата и [tableView reloadRowsAtIndexPaths:withRowAnimation]

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...