簡體   English   中英

UITableView無法正確顯示-為什么?

[英]UITableView doesn't appear correctly - why?

我使用Xcode 5和情節提要,在其中使用導航控制器和表格視圖,並在以下名為RootViewController.h代碼中,

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController
@property NSMutableArray *objects;
@end

RootViewController.m

#import "RootViewController.h"

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_objects addObjectsFromArray:@[@"Apple",@"Google",@"Intel"]];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%d", _objects.count);
    return _objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = @"A";

    return cell;
}

@end

Xcode說“ Build Succeeded”,但是結果屏幕上的行沒有正確顯示。
它返回空行和名為Root View Controller標題。

為什么?
我想我在numberOfRowsInSectioncellForRowAtIndexPathnumberOfSectionInTableView正確編寫了三個必需的方法,並且我認為不需要在頭文件中定義協議,因為它已經在父UITableViewController定義了。

並且由於某些原因,未執行numberOfRowsInSection方法中的調試NSLog函數。

那我在這里想念什么? 我該如何解決?

謝謝。

問題是_objects_objects當您嘗試將對象添加為_objects時,結果為_objects

因此,您需要創建數組而不是添加數組。 更改此行

[_objects addObjectsFromArray:@[@"Apple",@"Google",@"Intel"]];

對此

_objects = [NSMutableArray arrayWithObjects:@"Apple", @"Google", @"Intel", nil];

在情節提要中,您還需要設置UITableViewCell的單元格標識符(有關如何執行此操作的大量教程)。

放入viewDidLoad

self.title = @"some title";

更改NavigationBar的標題

您需要先創建對象,然后才能向其中添加任何內容:

- (void)viewDidLoad
{
    [super viewDidLoad];
    _objects = [NSMutableArray new];
    [_objects addObjectsFromArray:@[@"Apple",@"Google",@"Intel"]];
}

您是否設置了tableview委托? 您是否在情節提要中正確設置了tableviewcell?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM