Uitableviewcell内容无法点击

时间:2015-08-11 17:47:00

标签: ios objective-c uitableview

我是一位新手iOS开发人员用Objective-C语言编写我的应用程序。我发现自己面临一个问题,我搜索了很多,但没有结果:(

问题是:我以编程方式使用图像填充合适的视图单元格," eyelet"和一个标题,从在线抓取的新闻xml解析。

我要在这里下载的代码使得UI快照,但我已经找到原因并且我正在研究解决方案,所以没有问题,事实是这个代码加载内容,但是当我尝试用我之前制作的新闻细节打开第二个视图控制器时......好吧......单元格是不可点击的!

以下是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell;
        UILabel *eyeletLabel, *newsTitleLabel, *dateLabel;
        eyeletLabel = [UILabel new];
        newsTitleLabel = [UILabel new];
        newsImage = [UIImageView new];


    if (cell == nil) {
        //NSLog(@"Cell: %@", cell);
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        ATNews *thisNews = gNewsArray[indexPath.row];

        eyeletLabel.numberOfLines = 0;
        eyeletLabel.lineBreakMode = NSLineBreakByWordWrapping;
        eyeletLabel.frame = CGRectMake(90.0f, 5.0f, cell.contentView.frame.size.width - 20, 20);
        eyeletLabel.text = thisNews.eyelet;
        eyeletLabel.textColor = [UIColor orangeColor];

        newsTitleLabel.numberOfLines = 0;
        newsTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        newsTitleLabel.frame = CGRectMake(90.0f, 20.0f, cell.contentView.frame.size.width - 50, 50);

        newsTitleLabel.text = thisNews.title;
        NSDateFormatter *formatter = [NSDateFormatter new];
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSString *newDate = [formatter stringFromDate:thisNews.pubDate];
        dateLabel.text = [NSString stringWithString:newDate];

        for (id obj in thisNews.newsPhotos) {
            NSUInteger counter = 0;
            if ([obj isEqualToString: @"true"]) {
                counter = counter + 2;

                imageUrlFromArray = [thisNews.newsPhotos objectAtIndex:counter];
                NSURL *imageURL = [NSURL URLWithString:imageUrlFromArray];
                imageData = [NSData dataWithContentsOfURL:imageURL];
            }
            counter = counter + 3;
        }

        newsImage.frame = CGRectMake(5.0f, 5.0f, 70.0f, 70.0f);

        if (thisNews.newsPhotos.count != 0) {
            newsImage.image = nil;
            newsImage.image = [UIImage imageWithData:imageData];
            newsImage.contentMode = UIViewContentModeScaleAspectFill;
            newsImage.layer.cornerRadius = newsImage.frame.size.width / 2;
            [newsImage.layer setMasksToBounds:YES];
        }
        else {

            newsImage.image = [UIImage imageNamed:@"no-image.png"];
            newsImage.contentMode = UIViewContentModeScaleAspectFit;
            newsImage.layer.cornerRadius = newsImage.frame.size.width / 2;
            [newsImage.layer setMasksToBounds:YES];
        }

         [cell addSubview:newsImage];

         [cell addSubview:eyeletLabel];
         [cell addSubview:newsTitleLabel];
         [cell addSubview:dateLabel];
    }
    else {
    //NSLog(@"Cell");
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    }
    return cell;
}

我搜索了很多解决方案,我也在这里读了一些类似的问题,但没有成功,你能帮助我吗?非常感谢你!

1 个答案:

答案 0 :(得分:0)

实施委托方法

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

既然它是可点击的,你需要从你的单元格中添加一个segue到你的新视图控制器,或者从你的视图控制器创建一个segue到新的视图控制器并调用

[self.viewcontroller performSegueWithIdentifier:@"Your Identifer"];