首次加载数据后,UITableView为null,reloadData不起作用

时间:2013-09-10 12:44:01

标签: objective-c uitableview reloaddata

我有一个带有UITableView的自定义UIView。我通过界面构建​​器连接委托和dataSource,它看起来像这样:http://postimg.org/image/nj9elaj4h/(可能还没有上传图像:()

加载视图时,数据显示为原样。但是当我尝试调用reloadData时,没有任何反应。我检查了是否设置了uitableview,但它是NULL。但是,只要我拖动tableView并重新加载视图,就会显示新数据。任何人都知道为什么reloadData不起作用?

.h看起来像这样:

@interface NextTitleView :   UIView<UITableViewDataSource,UITableViewDelegate,SociusClientDelegate,UIActionSheetDelegate,Ne     xtTitleCustomCellDelegate>
 {
NexTitleCustomCell* _cellInFocus;
}

@property(nonatomic,retain)IBOutlet UITableView* _tableViewNextTitle;

@end

感谢您的帮助:D

编辑:添加.m文件

@implementation NextTitleView
@synthesize _tableViewNextTitle;
- (id)initWithFrame:(CGRect)frame
 {
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
}
return self;
}

  -(id)init
{
if(self)
{
    [SharedSingeltons sharedInstance]._client.delegateCustomClient = self;
    _tableViewNextTitle = [[UITableView alloc]init];
}

return self;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
return 2;
}

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == 0)
    return @"Now playing";
if(section == 1)
    return @"Comming Next";
else
    return @"";
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
    return 1;
if (section == 1)
    return [[SharedSingeltons sharedInstance]._client._musicList count];
else
    return 0;

  }

-(NexTitleCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{

NexTitleCustomCell* cell = [[NexTitleCustomCell alloc]init];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NextTitleCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

MusicItem* item = [[SharedSingeltons sharedInstance]._client._musicList
                   objectAtIndex:indexPath.row];
cell.delegate = self;
cell._labelSongTitle.text = item._songTitle;
cell._labelSongTitle.textColor = [UIColor blackColor];
cell._labelSubtitle.text = item._artist;
cell._identifier = item._songIdentifier;
cell._labelRating.text = item._rating.stringValue;

return cell;
 }

-(void)clientDidReceiveMusicList:(SociusClient *)sender list:(NSMutableArray *)array
  {
   for(MusicItem* item in [SharedSingeltons sharedInstance]._client._musicList)
{
    NSLog(@"rating:%@",item._rating);
}
NSLog(@"TAbleVIEW:%@",_tableViewNextTitle);
[self._tableViewNextTitle reloadData];

}

  -(void)didPressActionButton:(NexTitleCustomCell *)sender
    {
     NSLog(@"Show alert view");

   _cellInFocus = sender;
      UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil  delegate:self  cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Vote Up",  @"Like",@"Remember", nil];
[popupQuery showInView:self];
   }

  -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
   {
      if(buttonIndex == 0)
    {
    NSLog(@"Vote up");
    [self sendUpdateVoteForSong];
   }
   }

 -(void)sendUpdateVoteForSong
{
NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
[dict setObject:_cellInFocus._identifier forKey:@"Vote"];
NSLog(@"Identifier:%@",_cellInFocus._identifier);

[[SharedSingeltons sharedInstance]._client sendObject:dict error:nil];
 }



    @end

4 个答案:

答案 0 :(得分:0)

我会说您过早地调用重新加载数据,可能在initWithNibName,您应该在viewDidLoad中调用它。

答案 1 :(得分:0)

我只是设置了一个uiviewcontroller来处理视图。现在它工作正常。

答案 2 :(得分:0)

我认为UIActionSheet导致了这个问题,你可以尝试重新加载数据两次。

答案 3 :(得分:0)

  1. 我注意到你正在推销你的桌面视图,但它是一个Outlet ...你不需要为你的桌面视图分配init,如果它是一个IBOutlet 。因此要么摆脱IBOutlet,只是将其声明为属性(但是你需要确保以编程方式布置表视图),或者保留IBOutlet,并删除代码来分配init。目前您的数据源和代理也是从IB设置的,因此如果您为表格视图分配初始化,则不再设置那些...

  2. tableview出口不应该是零。

  3. 你能在awakeFromNib中放一个断点,看看TableView插座是否有NIL吗?

    您是否正确使用其NIB文件加载包含表视图的View类?

    如果您只是在Storyboard中嵌入View类,它将不会自动加载NIB。在这种情况下,我通常使用View容器,然后以编程方式加载NIB,并以编程方式将加载的视图嵌入到视图容器中。

    <table border="1" width="100%">
      <tr>
        <th colspan="3">Headline</th>
      </tr>
      <tr>
        <td>Content...</td>
        <td>Content...</td>
        <td>Content...</td>
      </tr>
    </table>
    

    此代码可以放在实用程序方法中,以便使用1行代码访问它。

    返回的Class将使IBOutlets从NIB连接到它的相应Class。

    现在检查调试器:

    NSArray *arrayXibObjects = [[NSBundle bundleWithIdentifier:@"net.mycompany.myappid"] loadNibNamed:@"myNibName" owner:nil options:nil];
    
    for (id object in arrayXibObjects) {
       if ([object isKindOfClass:myNibsClass]) {
           return object;   // this is your class loaded from NIB
       }  
    }
    

    然后将加载的NIB嵌入容器中,如下所示:(或使用约束)

    po [loadedClassFromNib tableView] // should not be NIL