tableview选中打勾

该博客介绍了一个使用Objective-C编写的iOS应用中,如何在UITableView中设置单元格选中状态,并记录用户的选中项。通过读取并更新.plist文件,初始化数据,然后在cellForRowAtIndexPath方法中处理单元格的选中效果,包括改变文字颜色和显示勾选图片。当用户选择新的单元格时,会更新当前选中项,保存选中状态,并播放对应的声音。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "FindMyPhoneTonesViewController.h"

#import "pelAppDelegate.h"

#import "PlistOOP.h"

#import "pelSmartWatchData.h"


@interface FindMyPhoneTonesViewController ()



@end


@implementation FindMyPhoneTonesViewController

static int current;

@synthesize names;

@synthesize keys;


@synthesize items;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.navigationItem.title = @"Find My Phone Tones";

    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] init];

barButtonItem.title = @"Back";

self.navigationItem.backBarButtonItem = barButtonItem;

    

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"plist"];

    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

    self.names = data;

    NSArray *array = [[names allKeys] sortedArrayUsingSelector:

                      @selector(compare:)];

    self.keys = array;


}




- (void)viewDidUnload {

    self.names = nil;

    self.keys = nil;

    [super viewDidUnload];

}


//kfq-----

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [names count];

}


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

{

    NSUInteger row = [indexPath row];

    NSString *key = [keys objectAtIndex:row];

    NSString*nameSection = [names objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:

SectionsTableIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:SectionsTableIdentifier];

    }

    cell.textLabel.text =nameSection;

    cell.selectionStyle=UITableViewCellSelectionStyleDefault;

  

    NSString *currString=[pelSmartWatchData loadDataByName:@"currentData"];

    current = [currString intValue];

    NSLog(@"instring:%d",current);


//默认选中上一次

    if(current==indexPath.row)

    {

        cell.imageView.image=[UIImage imageNamed:@"YES2.png"];

        cell.textLabel.textColor=[UIColor blueColor];

    }

    else

    {

        cell.accessoryType=UITableViewCellAccessoryNone;

    }

    

     //kfq---4-28

//    current=indexPath.row;

    return cell;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

//    [tableView reloadData];

    

    if(indexPath.row==current){

        return;

    }

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

    

       if(newCell.accessoryType==UITableViewCellAccessoryNone) {

        //        newCell.accessoryType=UITableViewCellAccessoryCheckmark;

        newCell.textLabel.textColor=[UIColor blueColor];

        

        newCell.imageView.image= [UIImage imageNamed:@"YES2.png"];

    }

    NSIndexPath*oldIndexPath =[NSIndexPath indexPathForRow:current inSection:0];

    UITableViewCell*oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];

    if(oldCell.imageView.image==[UIImage imageNamed:@"YES2.png"]) {

        oldCell.imageView.image=[UIImage imageNamed:@""];

        oldCell.accessoryType=UITableViewCellAccessoryNone;

        oldCell.textLabel.textColor=[UIColor blackColor];

//        newCell.imageView.image= [UIImage imageNamed:@"YES2.png"];

    }

    current=indexPath.row;

//    [self.FindMyPhoneToneTableView reloadData];

    [tableView reloadData];

    //kfq--4-29

    NSString *stringInt = [NSString stringWithFormat:@"%d",current];

    //kfq--5-4

    [pelSmartWatchData saveDataByName:@"currentData" data:stringInt];

    

    

    

    //kfq---4-25 保存选中声音的key

    NSUInteger row = [indexPath row];

    NSString *key = [keys objectAtIndex:row];


    [pelSmartWatchData saveDataByName:@"soundKey" data:key];

    [pelAppDelegate playSound:[key intValue]];



}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

完整的代码过程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值