簡體   English   中英

tableView iOS 上的選擇行

[英]Selection row on tableView iOS

我在 tableView 中選擇一行時遇到問題,問題是當我第一次選擇一行時它會以灰色突出顯示但沒有任何反應,我必須再次選擇一行來執行“didSelectRowAtIndexPath”並需要我到另一個 ViewController

// Number of rows in the tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
return 6;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 175.0;
}

// Populating each cell at a time.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    ClientCustomCell *cell = (ClientCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"ClientCustomCell" owner:self options:nil];
        cell = self.clientCustomCell;
        self.clientCustomCell = nil;

        cell.name = [nameArray objectAtIndex:indexPath.row];
        cell.number = [numberArray objectAtIndex:indexPath.row];
        cell.postal = [postalArray objectAtIndex:indexPath.row];
        cell.locality = [localityArray objectAtIndex:indexPath.row];
    }
    [cell setSelectionStyle:(UITableViewCellSelectionStyleBlue)];
    return cell;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"row selecter %d", (int)indexPath.row);
    InfoViewController *info = [[InfoViewController alloc] init];
    [self.navigationController pushViewController:info animated:YES];
}

您不小心實施了DESELECT rowAtIndexPAth,而不是SELECT rowAtIndexPath。

我發現您保留didDeselectRowAtIndexPath方法代替didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

使用以下代碼更新您的代碼:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"row selecter %d", (int)indexPath.row);
    InfoViewController *info = [[InfoViewController alloc] init];
    [self.navigationController pushViewController:info animated:YES];
}

你已經實現了didDeselectRowAtIndexPath ,這應該是didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

tableView iOS 上的選擇行

對於目標 c

   -(void)tableView:(UITableView *)tableView 
    didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

對於 Swift 4-5

 import UIKit

 extension UITableView {

func deselectSelectedRow(animated: Bool)
{
    if let indexPathForSelectedRow = self.indexPathForSelectedRow {
        self.deselectRow(at: indexPathForSelectedRow, animated: animated)
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM