简体   繁体   English

在选定的表视图单元格中执行NSRegularExpression操作

[英]Perform NSRegularExpression action in a selected tableview cell

I want to perform a NSRegularExpression action in a selected tableview cell. 我想在选定的表NSRegularExpression单元格中执行NSRegularExpression操作。 The problem is however, instead of preforming the action on the single selected item, it checks the entire array and then just blanks the answer "yes" on all items except websites (because of the separate "if" statements). 但是,问题是,它没有对单个选定的项目执行操作,而是检查整个数组,然后对除网站以外的所有项目都回答“是”(由于单独的“ if”语句)。

Here is the code: 这是代码:

NSArray *array = @"http://www.apple.com/, http://www.stackoverflow.com/, http://www.test.com/, This is text, 416-555-5555, 416-123-4567, This is also text.";
   //more code

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath* indexPath = [tableView indexPathForCell:sideSwipeCell];

    NSError *error = NULL;

    NSUInteger index = [buttons indexOfObject:button];
    NSDictionary* buttonInfo = [buttonData objectAtIndex:index];

    NSString *data = [[array objectAtIndex:indexPath.row] objectForKey:@"redirect_url"];

    NSRegularExpression *naNum = [NSRegularExpression regularExpressionWithPattern:@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$" options:NSRegularExpressionCaseInsensitive error:&error];
    NSRange naNumMatch = [naNum rangeOfFirstMatchInString:data options:0 range:NSMakeRange(0, [data length])];

    if ([data rangeOfString:@"http://"].location != NSNotFound) {
        // do something.
    } else if ((!NSEqualRanges(naNumMatch, NSMakeRange(NSNotFound, 0)))) {
        // do something
    } else {
        // do something
    }
}

How do I fix this? 我该如何解决? Thanks in advance. 提前致谢。

Why do you ignore the supplied indexPath, and create a new one? 为什么您忽略提供的indexPath并创建一个新的indexPath? That is almost for sure your problem. 几乎可以肯定这是您的问题。

If its not add some log messages to the three cases and log the range, what was matched, and the indexPath. 如果没有,则在这三种情况下添加一些日志消息,并记录范围,匹配项和indexPath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM