2013-12-16 436 views
2

我使用搜索欄的tableview。我在單元格中顯示數據的數量。我正在用xib創建單元格。我還在單元格中添加了兩個按鈕。現在,當我在搜索欄中搜索任何內容時,表格正在重新加載,單元格按鈕在該地點再次添加。 見下面的屏幕截圖: 1)第一次創建實現代碼如下:如何從單元格中刪除以前的內容

enter image description here

2)搜索數據後:

![enter image description here][2] 

enter image description here

我對這段代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
WorkDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkDetailCell"]; 
if (cell == nil) { 
    NSArray *topLevelObject; 
    topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"WorkDetailCell" owner:self options:nil]; 
    cell = [topLevelObject objectAtIndex:0]; 
    cell.backgroundColor=RGB(210, 200, 191); 
    cell.selectedBackgroundView=[self selectedCellView]; 
} 

if(search==FALSE){ 
    NSLog(@"%d",indexPath.row); 
    cell.clearsContextBeforeDrawing=YES; 
    //NSDictionary *detailList=[tempDataArray objectAtIndex:indexPath.row]; 
    NSArray *temp=[tempDataArray objectAtIndex:indexPath.row]; 
    cell.lblOrganization.text=[temp objectAtIndex:1];//@"TGB";//[detailList valueForKey:@"organizationName"]; 
    cell.lblAddress.text=[temp objectAtIndex:2];//@"Ahmedabad";//detailList[@"address"]; 
    cell.lblLandmark.text=[temp objectAtIndex:3];//@"SG HighWay";//detailList[@"landmark"]; 
    cell.lblCity.text=[temp objectAtIndex:4];//@"Ahmedabad";//detailList[@"city"]; 
    cell.lblState.text=[temp objectAtIndex:5];//@"Ahmedabad";//detailList[@"state"]; 


    UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame=CGRectMake(220, 45, 100, 60); 
    //[doneButton setTitle:@"pick" forState:UIControlStateNormal]; 
    [doneButton addTarget:self action:@selector(doneButtonClicked:) fo rControlEvents:UIControlEventTouchUpInside]; 
    [doneButton setImage:[UIImage imageNamed:@"pick"] forState:UIControlStateNormal]; 
    [doneButton setTag:indexPath.row]; 
    UIButton *previewButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    previewButton.frame=CGRectMake(235, 15, 65, 30); 
    [previewButton setTitle:@"Preview" forState:UIControlStateNormal]; 
    [previewButton setTitleColor:RGB(110, 73, 44) forState:UIControlStateNormal]; 
    [previewButton addTarget:self action:@selector(previewButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [previewButton setTag:indexPath.row]; 
    [cell.contentView addSubview:doneButton]; 
    [cell.contentView addSubview:previewButton]; 
} 
else{ 
// NSDictionary *detailList=[searchResultArray objectAtIndex:indexPath.row]; 
    // NSLog(@"dic%@",detailList); 
    //cell.contentView.clearsContextBeforeDrawing=YES; 
    cell.clearsContextBeforeDrawing=YES; 
    NSArray *temp=[searchResultArray objectAtIndex:indexPath.row]; 
    cell.lblOrganization.text= [temp objectAtIndex:1]; //[detailList valueForKey:@"organizationName"]; 
    cell.lblAddress.text=[temp objectAtIndex:2];//detailList[@"address"]; 
    cell.lblLandmark.text=[temp objectAtIndex:3];//detailList[@"landmark"]; 
    cell.lblCity.text=[temp objectAtIndex:4];//detailList[@"city"]; 
    cell.lblState.text=[temp objectAtIndex:5];//detailList[@"state"]; 
    UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame=CGRectMake(220, 45, 100, 60); 
    //[doneButton setTitle:@"pick" forState:UIControlStateNormal]; 
    [doneButton addTarget:self action:@selector(doneButtonClicked:) fo rControlEvents:UIControlEventTouchUpInside]; 
    [doneButton setImage:[UIImage imageNamed:@"pick"] forState:UIControlStateNormal]; 
    [doneButton setTag:indexPath.row]; 
    UIButton *previewButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    previewButton.frame=CGRectMake(235, 15, 65, 30); 
    [previewButton setTitle:@"Preview" forState:UIControlStateNormal]; 
    [previewButton setTitleColor:RGB(110, 73, 44) forState:UIControlStateNormal]; 
    [previewButton addTarget:self action:@selector(previewButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [previewButton setTag:indexPath.row]; 
    [cell.contentView addSubview:doneButton]; 
    [cell.contentView addSubview:previewButton]; 
    } 
    return cell; 

回答

1

你必須要擺脫掉或插入重複代碼的可重用性if(這是這nil值:if (cell == nil) {

這裏的主要規則是創造一個細胞,你必須分開2種對象: - 對象獨一無二的每一個細胞(例如文本) - 的重複對象(主要是風格的對象,如顏色,背景陰影,按鈕和東西)

第一組應「如果」 (cell == nil)在外面可重用性。第二個應該在裏面。

你的情況:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
WorkDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkDetailCell"]; 
if (cell == nil) { 
    NSArray *topLevelObject; 
    topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"WorkDetailCell" owner:self options:nil]; 
    cell = [topLevelObject objectAtIndex:0]; 
    cell.backgroundColor=RGB(210, 200, 191); 
    cell.selectedBackgroundView=[self selectedCellView]; 

    UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame=CGRectMake(220, 45, 100, 60); 
    //[doneButton setTitle:@"pick" forState:UIControlStateNormal]; 
    [doneButton addTarget:self action:@selector(doneButtonClicked:) fo rControlEvents:UIControlEventTouchUpInside]; 
    [doneButton setImage:[UIImage imageNamed:@"pick"] forState:UIControlStateNormal]; 
    [doneButton setTag:indexPath.row]; 
    UIButton *previewButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    previewButton.frame=CGRectMake(235, 15, 65, 30); 
    [previewButton setTitle:@"Preview" forState:UIControlStateNormal]; 
    [previewButton setTitleColor:RGB(110, 73, 44) forState:UIControlStateNormal]; 
    [previewButton addTarget:self action:@selector(previewButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:doneButton]; 
    [cell.contentView addSubview:previewButton]; 
} 

if(search==FALSE){ 
    NSLog(@"%d",indexPath.row); 
    cell.clearsContextBeforeDrawing=YES; 
    //NSDictionary *detailList=[tempDataArray objectAtIndex:indexPath.row]; 
    NSArray *temp=[tempDataArray objectAtIndex:indexPath.row]; 
    cell.lblOrganization.text=[temp objectAtIndex:1];//@"TGB";//[detailList valueForKey:@"organizationName"]; 
    cell.lblAddress.text=[temp objectAtIndex:2];//@"Ahmedabad";//detailList[@"address"]; 
    cell.lblLandmark.text=[temp objectAtIndex:3];//@"SG HighWay";//detailList[@"landmark"]; 
    cell.lblCity.text=[temp objectAtIndex:4];//@"Ahmedabad";//detailList[@"city"]; 
    cell.lblState.text=[temp objectAtIndex:5];//@"Ahmedabad";//detailList[@"state"]; 


    [previewButton setTag:indexPath.row]; 

} 
else{ 
// NSDictionary *detailList=[searchResultArray objectAtIndex:indexPath.row]; 
    // NSLog(@"dic%@",detailList); 
    //cell.contentView.clearsContextBeforeDrawing=YES; 
    cell.clearsContextBeforeDrawing=YES; 
    NSArray *temp=[searchResultArray objectAtIndex:indexPath.row]; 
    cell.lblOrganization.text= [temp objectAtIndex:1]; //[detailList valueForKey:@"organizationName"]; 
    cell.lblAddress.text=[temp objectAtIndex:2];//detailList[@"address"]; 
    cell.lblLandmark.text=[temp objectAtIndex:3];//detailList[@"landmark"]; 
    cell.lblCity.text=[temp objectAtIndex:4];//detailList[@"city"]; 
    cell.lblState.text=[temp objectAtIndex:5];//detailList[@"state"]; 

    [previewButton setTag:indexPath.row]; 
    } 
    return cell; 
+0

確定我想要的是同樣的事情...謝謝you..and我得到你的觀點 – jayesh

-1

,當你重新加載tableview中,你將需要刪除它們在你的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

添加爲子視圖 這樣的按鈕只需調用[cell.contentView removeSubViews];

這應該有幫助

+0

什麼是可重用的點,如果他這樣做呢? – Kuba

+0

他的dosent需要可重用性,如果你明白它不是可重用的問題,他每次都會添加內容,因此你可以刪除那些不需要的東西,所以按鈕不會像圖片中那樣添加兩次 – amrut1

+0

每個人都需要重用性!如果每次在滾動時都會出現可怕的滯後,那麼您的單元格就會變得多樣。每次都改變文字,重新繪製按鈕 - 不! – Kuba

0

你的代碼有兩個問題:

  1. 您可以設置標籤爲doneButtonpreviewButton但代碼不會被使用。那麼標籤用於什麼?

  2. 當您滾動表格視圖時,表格視圖單元格將被重新使用。例如,如果單元格出現在索引0處,在索引10處重用,則作爲代碼,這些按鈕將再次添加。

所以,你可以爲所有的按鈕分別設置一個唯一的標籤,每一次- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath被調用時,你應該檢查一下按鈕已被添加,如果是的話,你應該避免再次添加按鈕。

您可以像下面添加代碼:

if (![cell viewWithTag:YOUR_BUTTON_TAG]) { 
     //create and add your button 
    } 
相關問題