无法单击TableView单元格。(iOS,自定义单元实现)
我有一个带有文章标题列表的表视图。我已经实现了删除和归档这些文章的功能,可以通过向左滑动-归档,向右滑动-删除。它的工作原理非常类似于iPhone上名为Clear的应用程序。 然后,我被从按住ctrl键的单元格拖到新的视图控制器,在那里我有标签、imageview和textview来显示文章推送。然而,不知何故,单元格是不可点击的。我可以毫无问题地进行滑动,但不能单击并转到下一个视图控制器。我已使用-voidprepareForSegue:UIStoryboardSegue*segue sender:idsender设置要显示的数据。此外,我还使用教程来配置单元格。我也包括了这方面的实施。我想,这就是问题所在,但我不知道配置cell的代码是如何工作的。这种行为的原因是什么?多谢各位 InboxViewController.m的代码是具有tableview的代码:无法单击TableView单元格。(iOS,自定义单元实现),ios,objective-c,uitableview,tableview,Ios,Objective C,Uitableview,Tableview,我有一个带有文章标题列表的表视图。我已经实现了删除和归档这些文章的功能,可以通过向左滑动-归档,向右滑动-删除。它的工作原理非常类似于iPhone上名为Clear的应用程序。 然后,我被从按住ctrl键的单元格拖到新的视图控制器,在那里我有标签、imageview和textview来显示文章推送。然而,不知何故,单元格是不可点击的。我可以毫无问题地进行滑动,但不能单击并转到下一个视图控制器。我已使用-voidprepareForSegue:UIStoryboardSegue*segue send
. . .
#pragma mark view
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.db openDatabase];
NSString* date_added = [self.db getLastArticleDate];
[self makeConnetion:(id)date_added];
NSLog(@"viewWillAppear: self.articles: %d", self.articles.count);
[self.db closeDatabase];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.backgroundColor = [UIColor blackColor];
[self.tableView registerClass:[SHCTableViewCell class] forCellReuseIdentifier:@"Content"];
}
#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.
NSLog(@"numberOfRowsInSection: self.articles: %d", self.articles.count);
return self.articles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Content";
SHCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.backgroundColor = [UIColor clearColor];
//NSMutableArray* safeArticles = self.articles;
// Configure the cell...
Article* article = [self.articles objectAtIndex:indexPath.row];
NSString *listingKey = article.title;
NSString *listingValues = article.url;
cell.textLabel.text = listingKey;
cell.detailTextLabel.text = listingValues ;
cell.delegate = self;
cell.todoItem = article;
return cell;
}
#pragma mark cell atributes
-(UIColor*)colorForIndex:(NSInteger) index {
NSUInteger itemCount = self.articles.count - 1;
float val = ((float)index / (float)itemCount) * 0.6;
return [UIColor colorWithRed: 1.0 green:val blue: 0.0 alpha:1.0];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70.0f;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [self colorForIndex:indexPath.row];
}
#pragma mark TODO delete from server database
//method to delete an article form view and to call method to delete from database, as well as form server database
-(void)deleteArticle:(Article*)articleToDelete {
. . .
}
#pragma mark TODO archive in server database
//method to delete an article form view and to call method to delete from database, as well as form server database
-(void)archiveArticle:(Article*)articleToArchive {
. . .
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ArticleView"]) {
ArticleViewController* articleView = (ArticleViewController*) segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSInteger index = indexPath.row;
Article *article = [self.articles objectAtIndex:index];
articleView.article_title.text = article.title;
articleView.article_content.text = article.content;
}
}
SHCTableViewCell.m配置单元格
//
// SHCTableViewCell.m
// ClearStyle
//
// Created by Fahim Farook on 23/9/12.
// Copyright (c) 2012 RookSoft Pte. Ltd. All rights reserved.
//
#import "SHCTableViewCell.h"
#import <QuartzCore/QuartzCore.h>
@implementation SHCTableViewCell {
CAGradientLayer* _gradientLayer;
CGPoint _originalCenter;
BOOL _deleteOnDragRelease;
CALayer *_itemCompleteLayer;
BOOL _markCompleteOnDragRelease;
UILabel *_tickLabel;
UILabel *_crossLabel;
}
const float UI_CUES_MARGIN = 20.0f;
const float UI_CUES_WIDTH = 50.0f;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// add a tick and cross
_tickLabel = [self createCueLabel];
_tickLabel.text = @"Archive";
_tickLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:_tickLabel];
_crossLabel = [self createCueLabel];
_crossLabel.text = @"Delete";
_crossLabel.textAlignment = NSTextAlignmentLeft;
[self addSubview:_crossLabel];
// create a label that renders the todo item text
// remove the default blue highlight for selected cells
self.selectionStyle = UITableViewCellSelectionStyleNone;
// add a layer that overlays the cell adding a subtle gradient effect
_gradientLayer = [CAGradientLayer layer];
_gradientLayer.frame = self.bounds;
_gradientLayer.colors = @[(id)[[UIColor colorWithWhite:1.0f alpha:0.2f] CGColor],
(id)[[UIColor colorWithWhite:1.0f alpha:0.1f] CGColor],
(id)[[UIColor clearColor] CGColor],
(id)[[UIColor colorWithWhite:0.0f alpha:0.1f] CGColor]];
_gradientLayer.locations = @[@0.00f, @0.01f, @0.95f, @1.00f];
[self.layer insertSublayer:_gradientLayer atIndex:0];
// add a layer that renders a green background when an item is complete
_itemCompleteLayer = [CALayer layer];
_itemCompleteLayer.backgroundColor = [[[UIColor alloc] initWithRed:0.0 green:0.6 blue:0.0 alpha:1.0] CGColor];
_itemCompleteLayer.hidden = YES;
[self.layer insertSublayer:_itemCompleteLayer atIndex:0];
// add a pan recognizer
UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
}
return self;
}
const float LABEL_LEFT_MARGIN = 20.0f;
-(void)layoutSubviews {
[super layoutSubviews];
// ensure the gradient layers occupies the full bounds
_gradientLayer.frame = self.bounds;
_itemCompleteLayer.frame = self.bounds;
_tickLabel.frame = CGRectMake(-UI_CUES_WIDTH - UI_CUES_MARGIN, 0,
UI_CUES_WIDTH, self.bounds.size.height);
_crossLabel.frame = CGRectMake(self.bounds.size.width + UI_CUES_MARGIN, 0,
UI_CUES_WIDTH, self.bounds.size.height);
}
-(void)setTodoItem:(Article *)todoItem {
_todoItem = todoItem;
// we must update all the visual state associated with the model item
//_label.text = todoItem.text;
_itemCompleteLayer.hidden = !todoItem.completed;
}
// utility method for creating the contextual cues
-(UILabel*) createCueLabel {
UILabel* label = [[UILabel alloc] initWithFrame:CGRectNull];
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:15.0];
label.backgroundColor = [UIColor clearColor];
return label;
}
#pragma mark - horizontal pan gesture methods
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:[self superview]];
// Check for horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y)) {
return YES;
}
return NO;
}
-(void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
// if the gesture has just started, record the current centre location
_originalCenter = self.center;
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
// translate the center
CGPoint translation = [recognizer translationInView:self];
self.center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y);
// determine whether the item has been dragged far enough to initiate a delete / complete
_markCompleteOnDragRelease = self.frame.origin.x > self.frame.size.width / 2;
_deleteOnDragRelease = self.frame.origin.x < -self.frame.size.width / 2;
// Context cues
// fade the contextual cues
float cueAlpha = fabsf(self.frame.origin.x) / (self.frame.size.width / 2);
_tickLabel.alpha = cueAlpha;
_crossLabel.alpha = cueAlpha;
// indicate when the item have been pulled far enough to invoke the given action
_tickLabel.textColor = _markCompleteOnDragRelease ?
[UIColor greenColor] : [UIColor whiteColor];
_crossLabel.textColor = _deleteOnDragRelease ?
[UIColor redColor] : [UIColor whiteColor];
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
// the frame this cell would have had before being dragged
CGRect originalFrame = CGRectMake(0, self.frame.origin.y,
self.bounds.size.width, self.bounds.size.height);
if (!_deleteOnDragRelease) {
// if the item is not being deleted, snap back to the original location
[UIView animateWithDuration:0.2
animations:^{
self.frame = originalFrame;
}
];
}
if (_deleteOnDragRelease) {
// notify the delegate that this item should be deleted
[self.delegate deleteArticle:self.todoItem];
}
if (!_markCompleteOnDragRelease) {
// if the item is not being deleted, snap back to the original location
[UIView animateWithDuration:0.2
animations:^{
self.frame = originalFrame;
}
];
}
if (_markCompleteOnDragRelease) {
// mark the item as complete and update the UI state
self.todoItem.completed = YES;
[self.delegate archiveArticle:self.todoItem];
}
}
}
@end
如果要在单击tableview单元格后执行任何操作,请使用tableview委托方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"selected cell");
}
在禁用SHCTableViewCell中给出的手势识别器后,你能试试吗?嗨,如果我这样做也不行。我将尝试@007的方法。不管怎样,谢谢你的建议。你的想法是对的,我已经对SHCTableViewCell的整个实现进行了评论,现在,我可以选择一个用灰色突出显示的单元格。但是,它并没有转到其他视图。所以,部分问题可能在这个文件中。嗨,这确实输出了选定的单元格,但没有显示单元格已选定,我需要将数据转发到新视图,并弹出新视图。用这种方法可能吗?谢谢。首先你应该清楚你的疑问,你想要一些关于选择细胞的动画。