#ios #objective-c #uitableview #tableview
#iOS #objective-c #uitableview #tableview
Вопрос:
У меня есть табличное представление со списком названий статей. Я реализовал функции, которые будут удалять и архивировать эти статьи с помощью прокрутки (левый свайп — архив, правый свайп — удаление). Это отлично работает (например, приложение под названием «Очистить» на iPhone). Затем меня перетащили из ячейки (удерживая кнопку ctrl) в новый контроллер представления (где у меня есть label, imageview и textview), чтобы отобразить статью (нажать). Однако каким-то образом ячейки не доступны для просмотра. Я могу без проблем выполнять прокрутку, но не могу щелкнуть и перейти к следующему контроллеру представления. Я использовал - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
для настройки отображения данных. Кроме того, я использовал учебник для настройки ячейки. Я также включил реализацию для этого. Я полагаю, в этом проблема, но я не знаю, как работает код для настройки ячейки. В чем причина такого поведения? Спасибо.
Код для 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
Комментарии:
1. можете ли вы просто попробовать это после отключения распознавателя жестов, указанного в SHCTableViewCell.
2. Привет, это тоже не сработает, если я это сделаю. Я попробую метод @007. В любом случае спасибо за предложение.
3. Ваше мышление было правильным, я прокомментировал всю реализацию SHCTableViewCell, и теперь я могу выбрать ячейку (она выделяется серым цветом). Но, тем не менее, он не переходит в другое представление. Итак, часть проблемы, вероятно, в этом файле.
Ответ №1:
Используйте метод делегирования tableview, если вы хотите выполнить какое-либо действие после нажатия на ячейку tableview
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"selected cell");
}
Комментарии:
1. Привет, это выводит «выбранную ячейку», но нет визуальной индикации того, что ячейка выбрана, и мне нужно перенаправить данные в новое представление и открыть новое представление. Возможно ли это сделать с помощью этого метода? Спасибо.
2. сначала вы должны четко сформулировать свои сомнения, хотите ли вы анимацию при выборе ячеек.