2017-01-25 25 views
0

我知道當單元格隱藏時indexPath將返回nil,但我的問題是爲什麼它不可見?tableView:indexPathForCell在單元格中單擊時返回nil

我的情況是: 有一個在點擊按鈕時,電池的按鈕,並嘗試獲取indexPath(通過選擇indexPathForCell:)

同時,實現代碼如下被另一個線程刷新。

有時索引路徑會變爲零,我的問題是因爲按鈕在單元格中,當事件被觸發時,單元格必須存在(否則應如何點擊按鈕),導致單元格隱藏的內容?

thx。


//Cell.m 
@property (nonatomic, weak) UIButton *button; 
@property (nonatomic, weak) XXXDelegate *delegate; 

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    UIButton * button = xxx; 
    [self.contentView addSubview:button]; 

    [button addTarget:self action:@selector(xxxClick:) forControlEvents:UIControlEventTouchUpInside]; 
} 

-(void)xxxClick:(id)sender{ 
    [self.delegate xxxClick:self]; 
} 

//Controller.m 
-(void)loadView{ 
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; 
    self.tableView.delegate = self; 
    [self.tableView registerClass:[Cell class] forCellReuseIdentifier:@"xxx"]; 
}  

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xxx" forIndexPath:indexPath]; 
    //update cell 
    cell.delegate = self; 
    return cell; 
} 

- (void)xxxClick:(UITableViewCell*) cell { 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    //indexPath is nil sometimes? why? 
} 

而且還可以,實現代碼如下重載有時

+1

你的問題很混亂。你能提供更多的代碼嗎? – user6788419

+0

爲什麼自己是細胞在這裏,你應該通過視圖控制器添加目標 – Aakash

+0

添加一些示例代碼,thx – Henry

回答

1

也許你可以有它在故事板的自定義單元格定義錯誤重複使用的標識符。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell : SingleLabelTableCell = tableView.dequeueReusableCellWithIdentifier("SingleLabelTableCell", forIndexPath: indexPath) as! SingleLabelTableCell 
    let tempDict : NSDictionary = dataArray.objectAtIndex(indexPath.row) as! NSDictionary 
    cell.nameLabel.text = tempDict.valueForKey("title") as? String 
    return cell 
} 

或者你可以檢查一下這個視頻教程鏈接也

https://www.youtube.com/watch?v=OHue6nFUBO8

+0

thx,但沒有故事板,所有代碼都是由代碼繪製的 – Henry

0

THX的傢伙,我終於得到了答案。

當觸發按鈕並且tableview在同一個runloop中是「reloadData」時,「reloadData」會導致單元格不可見,而「indexPathForCell」將變爲零。

如果您不檢查indexPath的值,這很危險。

0

嘗試在使用以下格式

Cell.h

// 
// Cell.h 
// Projects 


#import <UIKit/UIKit.h> 

@interface Cell : UITableViewCell 


@property (weak, nonatomic) IBOutlet UIButton *btnCall; 

@end 

Cell.m

// 
// Cell.m 


#import "Cell.h" 


@implementation NearByDealDetailCell 

- (void)awakeFromNib { 
    [super awakeFromNib]; 


} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 

//控制器VC.m

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

     static NSString *simpleTableIdentifier = @"Cell"; 
     Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
     } 


       cell.btnCall.tag = indexPath.row; 
     [cell.btnCall addTarget:self action:@selector(actionBtnCall:) forControlEvents:UIControlEventTouchUpInside]; 

     return cell; 

} 



- (IBAction)actionBtnCall:(id)sender { 

    UIButton *btn = (UIButton *)sender; 
    NearByDealModel *activity = (NearByDealModel *)[self.activityList objectAtIndex:[btn tag]]; 

} 
相關問題