UITableView не отображает данные из массива - PullRequest
0 голосов
/ 12 ноября 2011

Код:

In .h:

    NSMutableArray *contentArray;

Я объявляю свой массив.


In .m

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        contentArray = [[NSMutableArray alloc] initWithObjects:@"view", @"browse", @"create", nil];

        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }

Я настраиваю его, по моему мнению, загрузил.

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return [contentArray count];
    }

Я устанавливаю количество строк для числа массивов.


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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }

        // Configure the cell...

        [[cell textLabel] setText:[contentArray objectAtIndex:indexPath.row]];

        return cell;
    }

Ничего.Но если я это сделаю "[[cell textLabel] setText: @" Hello World. "];"вместо этого он работает нормально.

Ответы [ 5 ]

1 голос
/ 12 ноября 2011

Вы хотя бы получаете 3 пустых строки в таблице?Если да, просто измените ваш код

NSString *tempString = [your_array objectAtIndex:indexPath.row];
cell.textLabel.text = tempString; 

Если вы даже не получаете пустые строки, сделайте свойство вашего массива в файле .h.Синтезируйте его в .m (также освободите его в функции delloc) и, наконец, в viewDidLoad сразу после

NSMutableArray tempContentArray = [[NSMutableArray alloc] arrayWithObjects:@"view", @"browse", @"create", nil];
self.contentArray=tempArray;

, а затем напишите следующий код, чтобы получить заголовок ячейки

NSString *tempString = [self.contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = tempString; 
0 голосов
/ 03 июля 2013

Убедитесь, что ваш .h реализует делегат табличного представления и источник данных ... должен выглядеть следующим образом

 @interface myClass : UITableViewController <UITableViewDelegate, UITableViewDataSource>

затем создайте этот код в вашем методе ViewDidLoad

 [self.tableView setDelegate:self];
 [self.tableView setDataSource:self];
0 голосов
/ 01 декабря 2012

просто используйте следующую строку кода // инициализируем nsarray с объектами.

-(void)viewDidLoad
{
NSArray *practice_ArrayForDisplayingEx=[NSArray alloc]initWithObjects:@"bhupi",@"bhupi",@"bhupi",@"bhupi",nil];

}


//then use in tableview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
   // UIButton *practice_takeTest_button;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]autorelease];
         }


    cell.textLabel.text =[practice_ArrayForDisplayingEx objectAtIndex:indexPath.row];
    cell.textLabel.font=[UIFont fontWithName:@"Arial" size:15];



    return cell;
}

убедитесь, что вы включили протокол UITableViewDelegate & UITableViewDataSourse в файл .h.

надеюсь, это поможет вам ..

0 голосов
/ 27 марта 2012

Ваш код в .m файле в порядке.Напишите свойство .h file, синтезируйте и освободите ваш файл массива NSMutable .m, это может вам помочь.

0 голосов
/ 12 ноября 2011

Попробуйте инициализировать ваш массив в методе init или вызовите reloadData на вашем UITableView после установки массива.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...