ios - 在 ViewController 中设置了 TableView 的数据源和委托(delegate),但没有任何反应

标签 ios uitableview uiviewcontroller uitabbarcontroller

我在 UIViewController 中有一个 UITableView,我想我已经完成了所有必要的步骤 设置它,但是当我把一个 NSLog 放在它的协议(protocol)的 requiered 方法中时,我没有得到 tableView 的响应

enter image description here

我告诉 UITableView 我将实现它的协议(protocol)

@interface SubSelectionTableViewWithMenuVC : UIViewController <CustomTableViewCellDelegate,
                                                                UITableViewDelegate,
                                                                UITableViewDataSource,
                                                                TableViewModelDelegate>

我在标题中设置了 tableView socket

@property (weak, nonatomic) IBOutlet UITableView  *tableView;

ViewDidLoad 中,我将委托(delegate)设置为 self。

self.tableView.delegate = self;
self.tableView.dataSource = self;

然后我实现了所需的委托(delegate)方法

– tableView:cellForRowAtIndexPath: 

– tableView:numberOfRowsInSection:  

但是没有任何反应,我在 tableView 对象的 ViewDidLoad 中做了一个 NSLog,它告诉我:

tableView: <UITableView: 0x1d43aa00; frame = (0 0; 0 0); clipsToBounds = YES;
autoresize = TM+BM; gestureRecognizers = <NSArray: 0x1cdc54f0>; layer = <CALayer:
0x1cdc5350>; contentOffset: {0, 0}>

必需的委托(delegate)方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"num of sections");
    return 1;
}

#pragma mark - UI set cell contents


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cell for row at indepath");
    static NSString *CellIdentifier = CELL_IDENTIFIER_LECTURES;
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.indexPath = indexPath;
    cell.delegate = self;

    // Configure cell
    [cell.mainLabel setText:[self titleForRow:indexPath.row]];
    [cell.numberLabel setText:[NSString stringWithFormat:@"%i", indexPath.row + 1]];
    cell.state = [self restoreTagButtonStateForCell:cell];

    // perform selector on cell to update and show its last stored state
    if ([cell respondsToSelector:@selector(updateButtonImageState)]) {
        [cell performSelector:@selector(updateButtonImageState)];
    } else {
        [NSException raise:NSGenericException format:@"cell does not respond to selector"];
    }

    // Setting the Highligthed color of selected cell
    UIView *goldenColor = [[UIView alloc] init];
    goldenColor.backgroundColor = [UIColor colorWithRed:0.824 green:0.749    blue:0.553 alpha:1.0f];
    cell.selectedBackgroundView = goldenColor;

    return cell;
}

最佳答案

原因是因为您没有实现– tableView:numberOfRowsInSection: 方法。实现此方法并返回您需要的金额。

关于ios - 在 ViewController 中设置了 TableView 的数据源和委托(delegate),但没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18477661/

相关文章:

ios - 在不创建新实例的情况下切换 View Controller

ios - 我如何创建一个与 "Cover vertical"相反的转换

ios - 在flutter iOS中加载基于ip的视频流

ios - 在 Swift 中从自定义 UITableViewCell 访问 UITableView

ios - tableView.beginUpdates 不适用于 iOS10

ios - 如何使用 View Controller 传回信息

ios - Twitter的嵌入式时间轴小部件不起作用

android - Android 上的 UIActivityViewController 等价物

ios - 具有错误的 IndexPath 影响的 UITableViewCells 和动画坐标问题

ios - 使用(自定义,交互式) View Controller 呈现和解雇处理 ScrollView