ios table实现单选

来源:互联网 发布:vb中sqr是什么意思 编辑:程序博客网 时间:2024/06/06 03:57

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


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

{

       return [tagsArraycount];

}


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

{

        static NSString * identifier=@"friendCell";

        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

        if(!cell)

        {

            cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

        }

        NSUInteger row = [indexPath row];

        NSUInteger oldRow = [lastIndexPathrow];

        [tableView cellForRowAtIndexPath:indexPath].accessoryType = (row == oldRow &&lastIndexPath != nil) ?UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

        

        cell.textLabel.text=[tagsArrayobjectAtIndex:indexPath.row];

        return cell;

    }



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

{

        int newRow = [indexPath row];

        int oldRow = [lastIndexPathrow];

        

        if ((newRow == 0 && oldRow ==0) || (newRow != oldRow))

{

            UITableViewCell *newCell = [tableViewcellForRowAtIndexPath:indexPath];

            newCell.accessoryType =UITableViewCellAccessoryCheckmark;


            UITableViewCell *oldCell = [tableViewcellForRowAtIndexPath: lastIndexPath];

            oldCell.accessoryType =UITableViewCellAccessoryNone;

            lastIndexPath=indexPath;

        }

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];

}



0 0