我在桌面视图中使用自定义单元格。我已经为单元设计创建了一个XIB,并将其元素挂钩到名为'Cell'的UITableViewCell子类。 XIB没有文件所有者。 此自定义单元格有三个标签视图。一个在左角,一个在中间,另一个在右角。其中,我不希望左右角的标签是可操作的,即这些标签不应触发任何被轻拍的动作。只有中间标签应该是可操作的,即在敲击时执行相关操作。
我已经在“viewDidLoad”方法中将XIB包注册到UITableViewController子类。这是代码段: -
Cell.h
#import <UIKit/UIKit.h>
@interface Cell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *authorLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *categoryLabel;
- (IBAction)authorTapped:(id)sender; //not actionable
- (IBAction)categoryTapped:(id)sender;//not actionable
@end
Cell.m
#import "Cell.h"
@implementation Cell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
NSLog(@"Cell init");
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (IBAction)authorTapped:(id)sender { //Do nothing on tapping left label
NSLog(@"Author tapped");
return;
}
- (IBAction)categoryTapped:(id)sender {//Do nothing on tapping right label
NSLog(@"Category tapped");
return;
}
@end
ListViewController.h
#import <Foundation/Foundation.h>
@interface ListViewController : UITableViewController
ListViewController.m
#import "ListViewController.h"
#import "Cell.h"
@implementation ListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
[self fetchEntries];
[[self navigationItem] setTitle:@"List"];
NSLog(@"%@",self.view);
}
return self;
}
-(void) viewDidLoad{
[super viewDidLoad];
NSLog(@"registering a xib for custom cell");
UINib *nib = [UINib nibWithNibName:@"Cell" bundle:nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:@"Cell"];
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Push the web view controller onto the navigation stack - this implicitly
// creates the web view controller's view the first time through
[[self navigationController] pushViewController:webViewController animated:YES];
// Grab the selected item
RSSItem *entry = [[channel items] objectAtIndex:[indexPath row]];
// Construct a URL with the link string of the item
NSURL *url = [NSURL URLWithString:[entry link]];
// Construct a request object with that URL
NSURLRequest *req = [NSURLRequest requestWithURL:url];
// Load the request into the web view
[[webViewController webView] loadRequest:req];
// Set the title of the web view controller's navigation item
[[webViewController navigationItem] setTitle:[entry title]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[channel items] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Table Cell processing");
Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"Cell"];
}
RSSItem *item = [[channel items] objectAtIndex:[indexPath row]];
[[cell titleLabel] setText:[item title]]; //Middle label (actionable)
[[cell authorLabel] setText:[item author]];//Left Label (non-actionable)
[[cell categoryLabel] setText:[item category]];//Right Label (non-actionable)
return cell;
}
在上面的代码片段中,已经以特殊方式配置已挂接到Cell对象的XIB文件。我添加了自定义适合按钮 - 一个位于左侧标签(标题)的顶部,另一个位于右侧标签(类别)的顶部,以使它们在整个单元格内部可分离地进行用户交互。这些按钮已设置为自定义类型,因此它们不会遮挡下方的角标签。 我已经创建了两个动作方法,它们应该通过Button对象(在角标签的顶部)作为消息传递给Cell对象。 然而,这两种方法甚至不是运行期间控制流程的一部分。当我点击单元格的角落(在这两个按钮上)时,整个单元格被选中并且 “ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath :( NSIndexPath *)indexPath“消息被传递。 这段代码我做错了什么?请帮助..我已经浪费了很多时间在这个简单的任务上。两个NSLog记录中没有一个 - “作者点击”和“类别点击”出现在控制台中..请帮助。