【问题标题】:Make the third visible row the only one selectable使第三个可见行成为唯一可选择的行
【发布时间】:2013-03-16 17:03:19
【问题描述】:

我正在使用最新的 SDK 开发 iOS 应用。

我有一个UITableView 有很多行,但我只显示五行。换句话说,用户只能看到五行。他/她可以滚动表格,但只能看到五行。

我想让第三行可选。用户只能选择第三个可见行。

我首先尝试模拟UIPickerView,因为我正在使用Core Data,现在所有代码都可以在UITableView委托中正常工作,其次,我不知道如何自定义UIPickerView背景和选择区域。

如何使第三个可见行成为唯一可选择的行?

【问题讨论】:

  • 可能还想让行对齐到第一个可见行的顶部。您可以使用 ScrollView 委托方法执行此操作。添加了一个示例。

标签: ios uitableview uipickerview


【解决方案1】:

像这样实现-[id<UITableViewDelegate> tableView:willSelectRowAtIndexPath:]

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *indexPaths = [tableView indexPathsForVisibleRows];
    if ([indexPaths objectAtIndex:2] isEqual:indexPath) {
        return indexPath;
    } else {
        return nil;
    }
}

更多信息here

【讨论】:

    【解决方案2】:

    完整示例以及对齐行(通过实现 ScrollView 委托):

    ViewController.h:

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
        @property (weak, nonatomic) IBOutlet UITableView *myTableView;
    @end
    

    ViewController.m:

    #import "ViewController.h"
    
    static int kRowHeight = 92;
    static int kArrayCount = 20;
    
    @interface ViewController ()
    @property (nonatomic, strong) NSMutableArray *tableViewData;
    
    @end
    
    @implementation ViewController
    -(void)populateArray {
        for (int i=0; i<kArrayCount; i++) {
            [self.tableViewData addObject:[NSString stringWithFormat:@"String # %d",i]];
        }
        [self.myTableView reloadData];
    }
    
    #pragma mark - Table view data source
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return [self.tableViewData count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.textLabel.text = [self.tableViewData objectAtIndex:indexPath.row];
        // Configure the cell...
    
        return cell;
    }
    
    #pragma mark - Table view delegate
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.
        // Standard UIAlert Syntax
        UIAlertView *myAlert = [[UIAlertView alloc]
                                initWithTitle:@"Selected"
                                message:[NSString stringWithFormat:@"Selected Row %d", indexPath.row]
                                delegate:nil
                                cancelButtonTitle:@"OK"
                                otherButtonTitles:nil, nil];
    
        [myAlert show];
    
    }
    
    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSArray *indexPaths = [tableView indexPathsForVisibleRows];
        if ([[indexPaths objectAtIndex:2] isEqual:indexPath]) {
            return indexPath;
        } else {
            return nil;
        }
    }
    
    
    #pragma mark - ScrollView Delegate Methods (Make rows snap to position top)
    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                         withVelocity:(CGPoint)velocity
                  targetContentOffset:(inout CGPoint *)targetContentOffset {
        int cellHeight = kRowHeight;
        *targetContentOffset = CGPointMake(targetContentOffset->x,
                                           targetContentOffset->y - (((int)targetContentOffset->y) % cellHeight));
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.tableViewData = [[NSMutableArray alloc] initWithCapacity:kArrayCount];
        [self populateArray];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    @end
    

    【讨论】:

      【解决方案3】:

      您可以使用数组[self.tableView indexPathsForVisibleRows] 找出可见的indexPaths。在 willSelectRowAtIndexPath 方法中,除了第三个元素之外的所有元素都返回 nil

      为了避免突出显示,您也可以使用它来设置

      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      

      在 CFRAIP 方法中。

      【讨论】:

        猜你喜欢
        • 2017-02-24
        • 1970-01-01
        • 1970-01-01
        • 2019-11-28
        • 2013-02-18
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 2013-12-19
        相关资源
        最近更新 更多