如何删除目标c中的表格单元值?

我尝试的代码,但它崩溃在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method cell.adLabel.text = [array objectAtIndex:[indexPath row]]; 线。

- (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 %lun", 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];
}


}

我该如何解决这个问题? 是否有可能,消失的表格视图数据或删除?


另一个选择是不从数据源中移除项目,而是修改该行的高度以动态地隐藏它。 你可以通过使用- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath委托方法和[tableView reloadRowsAtIndexPaths:withRowAnimation]


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

在视图显示之前调用,所以你不会有机会按下按钮和调用

- (IBAction)tablodaGosterBtnClick:(id)sender

因此,您应该将数组初始化移动到其他位置。 如果你使用数组作为你的表视图的数据源,那么一个好的地方是

-(void)ViewDidLoad

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
}
链接地址: http://www.djcxy.com/p/60493.html

上一篇: How can I remove table cell values in objective c?

下一篇: Xcode 4.2 crashing on launch