2014-06-23 2 views
0

기사 제목 목록이있는 표보기가 있습니다. 스 와이프 (왼쪽 스 와이프 - 아카이브, 오른쪽 스 와이프 - 삭제)로 해당 기사를 삭제하고 보관하는 기능을 구현했습니다. 그것은 (아이폰에서 "Clear"라는 어플리케이션과 같이) 훌륭하게 작동합니다. 그런 다음 셀 (Ctrl 키를 누른 상태)에서 새 뷰 컨트롤러 (label, imageview 및 textview가있는 곳)로 드래그하여 기사를 표시합니다 (푸시). 그러나 어떻게 든 셀은 클릭 할 수 없습니다. 문제없이 스 와이프를 할 수는 있지만 클릭하고 다음보기 컨트롤러로 이동할 수는 없습니다. 데이터를 표시하기 위해 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender을 사용했습니다. 또한 튜토리얼을 사용하여 셀을 구성했습니다. 나는 그것을위한 구현도 포함시켰다. 나는 그것이 문제가되는 곳이라고 생각하지만, 셀을 구성하는 코드가 어떻게 작동하는지 모르겠다. 그러한 행동의 원인은 무엇입니까? 고맙습니다.TableView 셀을 클릭 할 수 없습니다. (iOS, 맞춤 셀 구현)

InboxViewController.m에 대한 코드 (의 tableview를 가지고 한) : 당신이 클릭 한 후 모든 작업을하려는 경우

. . . 

#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 
+0

SHCTableVi에 지정된 제스처 인식기를 비활성화 한 후에 사용해 볼 수 있습니까? ewCell. – jithinroy

+0

안녕하세요, 그 중 하나를 수행하면 작동하지 않습니다. @ 007 방법을 시도합니다. 어쨌든 제안 해 주셔서 감사합니다. – sermilion

+0

당신의 생각은 옳았습니다. SHCTableViewCell의 전체 구현을 주석 처리했으며 현재 셀을 선택할 수 있습니다 (회색으로 강조 표시됨). 그러나 여전히, 그것은 다른 견해로 가지 않습니다. 그래서이 파일에서 문제의 일부일 것입니다. – sermilion

답변

0

사용의 tableview 위임 방법 (셀을 구성) 표 뷰 셀

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

{ 
    NSLog(@"selected cell"); 
} 
+0

안녕하세요, 이것은 "선택한 셀"을 출력하지만 셀이 선택되었고 새로운보기로 데이터를 전달하고 새보기를 팝업해야한다는 시각적 표시가 없습니다. 이 방법으로 가능합니까? 고맙습니다. – sermilion

+0

먼저 의심을 분명히해야합니다. 셀을 선택하는 데 애니메이션이 필요합니까? – morroko

관련 문제