【问题标题】:Have set datasource and delegate of TableView inside ViewController but nothing happening在 ViewController 中设置了 TableView 的数据源和委托,但没有任何反应
【发布时间】:2013-08-28 00:44:07
【问题描述】:

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

我告诉 UITableView 我将实现它的协议

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

我在标题中设置了 tableView 插座

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

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

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

我实现了所需的委托方法

– 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}>

必需的委托方法:

- (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;
}

【问题讨论】:

  • 你在 UITableView 的数据源方法中有什么?
  • 更新了问题。我注意到一些奇怪的东西,有时是 NSLogs cellForRowAtIndexPath 有时是 numberOfSectionsInTableView: never both
  • 返回多少行?
  • @AbdullahShafique:捂脸! 0 呵呵 - 问题解决了。

标签: ios uitableview uiviewcontroller uitabbarcontroller


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多