没有调用Objective-c UITableView单独的类cellForRowAtIndexPath [英] Objective-c UITableView separate class cellForRowAtIndexPath does not get called
问题描述
与该主题相关的问题很多,但是我还没有遇到用例,因此请继续。
There are a huge number of questions relating to this topic but I have not yet come across my use case so here goes.
这是我在OBJ-C的头几周,所以我不知道我在用这些东西做什么...
This is my first couple weeks in OBJ-C so I have no clue what I am doing with some of this stuff...
我想要的
我不太喜欢在OBJ-C中看到如此多的类而使视图控制器超载具有地球上所有功能的类。就OOP而言,它看起来很脏,感觉很粗糙。在我的用例中,我没有全屏桌子,只有一个小桌子可容纳10件东西。因此,使用完整的UITableViewController非常不合适。相反,我希望所有我的表委托特定方法都在UITableView子类中。不在UITableViewController或具有UITableView属性的ViewController中。这应该是非常简单的……
I do not particularly enjoy seeing so many classes in OBJ-C that overload the view controller classes with every and any function on this earth. It looks dirty and feels gross as far as OOP goes. In my use case I don't have a full screen table just a little one to hold 10 things. Therefore it's quite inappropriate to use a full UITableViewController. Instead, I want to have all my table delegate specific methods to be in a UITableView sub-class. NOT in a UITableViewController or a ViewController with a UITableView property. This should be mega simple yet...
问题
我该怎么办似乎无法触发方法 cellForRowAtIndexPath
。我知道足够多,这些东西在很大程度上依赖于委托和数据源分配...但是,因为我有一个单独的UITableView类,该类使用< UITableViewDelegate,UITableViewDataSource>
委托我认为我根本不必做任何作业!
No matter what I do I cannot seem to get the method cellForRowAtIndexPath
to fire. I know enough to know that this stuff relies heavily on the delegate and datasource assignment... however since I have a separate UITableView class that uses the <UITableViewDelegate, UITableViewDataSource>
delegations I don't think I should have to do any sort of assignment at all!
我要写什么??? self.delegate =自我
?或更糟糕的是,在调用此UITableView类的ViewController中, self.tasksTable.delgate = self.tasksTable
吗?哎呀...总额
What am I gonna write?? self.delegate = self
? or worse, in the ViewController that calls this UITableView class, self.tasksTable.delgate = self.tasksTable
? Eww... gross
这是我在代码中所做的事情。
Here is what I am doing in code.
代码
TasksTableView.h
#import <UIKit/UIKit.h>
@interface TasksTableView : UITableView <UITableViewDelegate, UITableViewDataSource> {
NSArray *tasksData;
}
- (NSMutableArray *)getAllTasks;
@end
TasksTableView.m
#import "TasksTableView.h"
#import "NSObject+RemoteFetch.h" //<--I use this to fetch, obvs
@interface TasksTableView ()
@property (nonatomic, strong) NSString *cellId;
@end
@implementation TasksTableView
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if(self) {
_cellId = @"AllTasksTableCell";
tasksData = [self getAllTasks];
}
return self;
}
#pragma mark - Custom Table Functionality
- (NSMutableArray *)getAllTasks {
@try {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *TASKS_URL = [userDefaults objectForKey:@"tasksUrl"];
NSObject *fetcher = [[NSObject alloc] init];
NSDictionary *response = [fetcher fetchAPICall:TASKS_URL httpRequestType:@"GET" requestBodyData:nil];
return [response objectForKey:@"data"];
} @catch (NSException *exception) {
NSLog(@"could not get tasks, error: %@", exception);
return nil;
}
}
#pragma mark - UITableView DataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tasksData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//<-- NEVER GETS HERE
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_cellId];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:_cellId];
}
cell.textLabel.text = [tasksData objectAtIndex:indexPath.row];
return cell;
}
@end
我也很难受时间来确定要设置为数据源的内容。在其他语言中,通常会使用 self.DataSource = [self getAllTasks]
设置DataSource对象,但是到目前为止,我所做的所有教程都倾向于使用一些奇怪的东西。然后使用临时NSArray或NSDictionary将表函数的索引与数组或字典键的索引相关联...这使我非常困惑,为什么我不能只设置DataSource对象并使表知道要进行迭代在数据上。
I am also having a hard time figuring out what to set as the datasource. In other languages you would typically set the DataSource object with self.DataSource = [self getAllTasks]
... however all the tuturials I have done thus far all tend to use some weird ad-hoc NSArray or NSDictionary to then correlate the index of the table functions with the index of the array or dictionary keys... This confuses me greatly as to why I can't just set the DataSource object and have the table know to iterate over it's data.
我的结论是不会触发,因为它认为DataSource对象为空并且没有行? (确实如此,但是就像我说的那样,人们似乎可以使Tables在YouTube上正常运行)
My conclusion is that this isn't firing because it thinks the DataSource object is empty and there are no rows? (which it is, but like I said people seem to get Tables to work fine on YouTube doing this)
谢谢。
推荐答案
TasksTableView
类是从 UITableView
类&您正在同一类中实现 UITableview
委托。
TasksTableView
class is derived from UITableView
class & You are implementing the UITableview
delegates in the same class. This will not work.
而不是创建 UITableView
子类。创建 TasksTableView
类作为 NSObject
子类。并从添加了 tableview
的位置传递 tableview
对象。
Instead of creating a UITableView
subclass. Create TasksTableView
class as NSObject
sub class. And pass the tableview
object from where you added a tableview
.
@interface TasksTableView : NSObject <UITableViewDelegate, UITableViewDataSource> {
NSArray *tasksData;
__weak UITableView *tableView;
}
并将该表视图委托设置为 self
(TasksTableView对象),而 inits
TasksTableView
类
And set that table view delegate to self
(TasksTableView object) while init
the TasksTableView
Class
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if(self) {
_cellId = @"AllTasksTableCell";
tasksData = [self getAllTasks];
self.tableView.delgate = self;
self.tableView.datasource = self;
}
return self;
}
现在您的委托
方法将针对该特定 tableview
Now your delegate
methods will trigger for that specific tableview
这篇关于没有调用Objective-c UITableView单独的类cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!