2013-05-29 122 views
1

看來我的tableview委託不會顯示我的圖像。該圖像顯示在我的視圖控制器添加到我的UIImageView罰款。如果這一點信息有幫助,我正在使用接口構建器的表視圖。UITableView不顯示圖像

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


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

cell.accessoryView = [[UIImageView alloc] initWithImage:myimage]; 

return cell; 

}

回答

1

嘗試通過interface builder設置委託和數據源,或者在viewDidLoad方法。

-(void)viewDidLoad{ 
self.tableView.delegate = self; 
self.tableView.dataSource = self; 
//... 
} 

此外,還要確保myImage對象不是nil

,如果我能記錯的話,default風格有一個ImageView的屬性。嘗試:

cell.imageView.image = [UIImage imageNamed:@"image.png"]; 
+0

我嘗試這樣做,但我得到這個錯誤。 - [ViewController tableView:numberOfRowsInSection:]:無法識別的選擇器發送到實例0x81ec820 –

+1

確保您正在調用'numberOfRowsInSection:'方法。並返回您在陣列中擁有的對象數量。 – LAMBORGHINI

+0

'cell.imageView.image = [UIImage imageNamed:@「image.png」];'是做到這一點的正確方法 –

1

您需要設置委託和數據源在您的viewDidLoad方法,並在實現代碼如下本身的委託方法代替你的.h文件中。

編輯:

另外,還要確保你所有的委託方法添加

這些代表是:

// Return the number of sections 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

// Return the number of rows 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 

//filling of tableview 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 

//pressing of a row 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 
+0

我嘗試這樣做,但我得到這個錯誤 - [視圖控制器的tableView:numberOfRowsInSection:]:無法識別的選擇發送到實例0x81ec820 –