程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-在滾動表視圖時選擇目錄重復

ios-在滾動表視圖時選擇目錄重復

編輯:編程綜合問答
在滾動表視圖時選擇目錄重復

在一個表視圖中顯示信息,信息是固定的。使用邏輯將選擇目錄選擇特定的空間。代碼如下:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
      *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";
    // intializing tableview cell.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier] autorelease];
    }
    // Setting tableviewcell title from Room array.

    cell.textLabel.text = [NSString stringWithFormat:@"%@",[[arr_roomList
     objectAtIndex:indexPath.row] valueForKey:@"description"]];

    /* checking the condition if checkedIndexPath is != null.
     means at first time checkedIndexPath= null.*/
    if (self.checkedIndexPath) {

        /* checking the condition if current cell is selected then
         we have to show the UITableViewCellAccessoryCheckmark (checkmark on right side of the
        cell).*/
        if([self.checkedIndexPath isEqual:indexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    else{
        if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:@"resource_id"]
       isEqualToString:self.str_selected_resourceId]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    // This Method will set the Background and Selected Background Image for the cell.
    [[AppDelegate sharedInstance] SetBackgroundImageForcell:cell];
    if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:@"rec_type"] isEqualToString:@"R"]) {
        cell.backgroundColor = [UIColor grayColor];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    /* checking the condition if checkedIndexPath is != null.
     means at first time checkedIndexPath= null.*/
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
     cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
    [self changeRoomWithResourceId:[[arr_roomList objectAtIndex:indexPath.row] 
    valueForKey:@"resource_id"]];
  }

在往下滾動表視圖時,選擇目錄會隨機在單元中自身重復。請幫忙,謝謝。

最佳回答:


   if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:@"resource_id"]
   isEqualToString:self.str_selected_resourceId]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

checkedIndexPath為nil時 多個room的resource_id 都等於self.str_selected_resourceId 會導致
多個cell被標記check mark

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved