ios開發之--tableview單選實現

來源:互聯網
上載者:User

標籤:led   def      seq   inf   tab   setvalue   name   class   

實現思路比較簡單,這裡僅做記錄:

直接上代碼:

1,實現didSelectRowAtIndexPath方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    [[NSUserDefaults standardUserDefaults]setValue:[array objectAtIndex:indexPath.row] forKey:APP_CHANGEVOICE];        [_sextTableView reloadData];    [tableView deselectRowAtIndexPath:indexPath animated:YES];    }

在cellForRowAtIndexPath裡面實現方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellTableIdentifier = @"CellTableIdentifier";    hPublickCell *cell = [tableView cellForRowAtIndexPath:indexPath];    if (cell == nil)    {        cell = [[hPublickCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellTableIdentifier];        cell.selectionStyle = UITableViewCellSelectionStyleNone;    }        cell.textsLabel.text = array[indexPath.row];    cell.selectionStyle=UITableViewCellSelectionStyleGray;    //選擇狀態的儲存    if ([[[NSUserDefaults standardUserDefaults]valueForKey:APP_CHANGEVOICE] isEqualToString:[array objectAtIndex:indexPath.row]])    {        cell.accessoryType = UITableViewCellAccessoryCheckmark;    }    else    {        cell.accessoryType = UITableViewCellAccessoryNone;    }        return cell;    }

這裡面的array是資料來源數組。如下:

 

 

2,上面這種是系統的選中樣式,下面是自訂的:

代碼如下:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    ShippingAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShippingAddressCell"];    if (!cell) {        cell = [[ShippingAddressCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ShippingAddressCell"];    }        if (self.lastIndexPath == indexPath)    {        cell.selectedImg.image = [UIImage imageNamed:@"clicked"];    }else    {        cell.selectedImg.image = [UIImage imageNamed:@"unClick"];    }        return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    //之前選中的,取消選擇    ShippingAddressCell *celled = [tableView cellForRowAtIndexPath:_lastIndexPath];    celled.selectedImg.image = [UIImage imageNamed:@"unClick"];    //記錄當前選中的位置索引    _lastIndexPath = indexPath;    //當前選擇的打勾    ShippingAddressCell *cell = [tableView cellForRowAtIndexPath:indexPath];    cell.selectedImg.image = [UIImage imageNamed:@"clicked"];}

這樣就可以實現了,如下:

 

多選的有空再完善!代碼可以直接粘貼使用!

 

ios開發之--tableview單選實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.