UIView中的UITableView - 只有在uitableview外滚动单元格时才会加载数据

时间:2014-11-28 08:44:01

标签: ios objective-c uitableview

我在UITableView内使用UIViewController时遇到问题: 当我启动应用时,会显示UITableView,但不会显示来自_opponents的数据。 当我开始在UITableView之外滚动第一个单元格时,现在此单元格会更新。 此外,当我手动触发[_topponents reloaddata]时,整个UITableView显示正确的数据。

我需要添加或更改哪些内容,以便从头开始显示数据?

dataSourcedelegate都与故事板中的类相关联

这是我的代码:
viewDidLoad:

- (void)viewDidLoad {
  [super viewDidLoad];
  myappdel = (AppDelegate *)[UIApplication sharedApplication].delegate;
  self.title = @"New Match";
  _lopponent.text = _opponent;

  opponents = [[NSMutableArray alloc] init];
  NSMutableDictionary *opponent = [[NSMutableDictionary alloc] init];
  [opponent setObject:@"opponent A" forKey:@"name"];
  [opponent setObject:@"xx matches | xx wins || xx losts" forKey:@"stats"];
  [opponent setObject:@"Krems" forKey:@"location"];

  NSMutableDictionary *opponent2 = [[NSMutableDictionary alloc] init];
  [opponent2 setObject:@"opponent B" forKey:@"name"];
  [opponent2 setObject:@"yy matches | yy wins || yy losts" forKey:@"stats"];
  [opponent2 setObject:@"Location B" forKey:@"location"];

  NSMutableDictionary *opponent3 = [[NSMutableDictionary alloc] init];
  [opponent3 setObject:@"opponent C" forKey:@"name"];
  [opponent3 setObject:@"zz matches | zz wins || zz losts" forKey:@"stats"];
  [opponent3 setObject:@"Location C" forKey:@"location"];

  [opponents addObject:opponent];
  [opponents addObject:opponent2];
  [opponents addObject:opponent3];

  [_topponents reloadData];

}

这里是我的cellForRowAtIndexPath:

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



UILabel *lopponentname = (UILabel*)[cell viewWithTag:1];
UILabel *lopponentstats = (UILabel*)[cell viewWithTag:2];
UILabel *llocation = (UILabel*)[cell viewWithTag:3];
lopponentname.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"name"];
lopponentstats.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"stats"];
llocation.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"location"];


return cell;
}

5 个答案:

答案 0 :(得分:2)

我发现了我的错!

我以前写过:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]

但我应该使用

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

使用" forIndexPath"

答案 1 :(得分:0)

我认为,问题在于细胞。创建UITableViewCell子类。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"opponentcell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.lopponentname.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.lopponentstats.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"stats"];
cell.llocation.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"location"];
return cell;
}

希望它对你有所帮助。

答案 2 :(得分:0)

在细胞初始化后添加。对我来说工作正常。

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

答案 3 :(得分:0)

当细胞为零时,您需要检查条件。 最初的细胞是零.. 所以数据只在滚动后才加载...... 初始化单元格后,在cellForRowAtIndexPath委托方法中添加以下代码行。

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

希望这可以解决问题......

答案 4 :(得分:0)

我认为您正在使用自定义cel,看起来您在“cellForRowAtIndexPath”中遇到了麻烦。如果您使用.xib,您可能会发现它很有用。

static NSString *cellIdentifier = @"opponentcell";    

CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        NSArray *nibObjects =[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:nil options:nil];
        for (id currentObject in nibObjects)
        {
            if ([currentObject isKindOfClass:[CustomTableViewCell class]])
            {
                cell = (CustomTableViewCell *) currentObject;
            }
        }
        [cell initViewStyles];
    }
相关问题