2010-01-26 79 views
1

我做了一個應用程序,它具有從相機捕獲圖像和點擊按鈕的按鈕,pickerView將通過操作表顯示並選擇圖像類型(jpg,png,bmp ..),一旦選擇從選擇器,按鈕標題將根據pickerView結果改變,uItableviewcell動態檢索值

然後我嘗試將此轉換爲UITableView的,其具有用於圖像選擇的圖像的類型2部分(1和1,

現在我'有問題,我嘗試imageView到單元格中,但圖像重疊其他單元格,並且一旦圖像捕獲從相機imageView未更新,但一旦滾動表格,只有更新,

的另一個問題是,一旦選擇第二部分我能夠調用pickerView選擇圖像類型如以前,但一旦從pickerView選擇我沒怎麼更新cell.text

下面

是我的代碼,任何人都可以在這裏幫助或改善它,

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 1; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    switch (section) { 
     case 0: 
      return NSLocalizedString(@"Image", @""); 
      break; 
     case 1: 
      return NSLocalizedString(@"Image type", @""); 
      break; 
     default: 
      break; 
    } 
} 

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease]; 
    } 

    switch (indexPath.section) { 
     case 0: 
     { 
      UIImageView *abc = [[UIImageView alloc] initWithFrame:CGRectMake(5,5,100,100)]; 
      abc.image = imageView.image; 
      [cell addSubview:abc]; 
     } 
      break; 
     case 1: 
      cell.textLabel.text = @"- Click here -"; 
      break; 
     default: 
      break; 
    } 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease]; 
    } 

    if(indexPath.section == 0) { 
     NSLog(@"selected %d",indexPath.section); 
    } 
    if(indexPath.section == 1) { 
     NSLog(@"selected %d",indexPath.section); 
     [self chooseType]; 
    } 
} 

- (void)chooseType{ 
    actionSheet = [[UIActionSheet alloc] initWithTitle:@"" 
               delegate:self 
            cancelButtonTitle:nil 
           destructiveButtonTitle:nil 
            otherButtonTitles:nil]; 

    NSArray *typeObject = [NSArray arrayWithObjects:@".jpg",@".png",@".bmp",nil]; 

    self.pickerData = typeObject; 

    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)]; 
    pickerView.delegate = self; 
    pickerView.showsSelectionIndicator = YES; 

    pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    pickerDateToolbar.barStyle = UIBarStyleBlackOpaque; 
    [pickerDateToolbar sizeToFit]; 

    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    [barItems addObject:flexSpace]; 
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(TypeDonePick)]; 
    [barItems addObject:doneBtn]; 

    [pickerDateToolbar setItems:barItems animated:YES]; 

    [actionSheet addSubview:pickerDateToolbar]; 
    [actionSheet addSubview:pickerView]; 
    [actionSheet showInView:self.view]; 
    [actionSheet setBounds:CGRectMake(0,0,320, 464)]; 
    [pickerView release]; 
    [actionSheet release]; 
} 


- (void)TypeDonePick 
{ 
    NSInteger row = [pickerView selectedRowInComponent:0]; 
    titleCause.textColor = [UIColor blackColor]; 
    titleCause.text = [pickerData objectAtIndex:row]; 
    NSLog(@" %@ ",titleCause.text); 
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
} 

是有可能電池測試從外部範圍UITable功能

感謝

+1

請編輯您的帖子把代碼中的代碼標記,使我們可以更容易地閱讀它。 – 2010-01-26 06:53:26

回答

1

你的概率變lem是表不知道它需要更新已經可見的單元格。對於快速解決方案,只需在用戶選擇圖像/圖像類型後添加[tableView reloadData],這將導致表格重新創建單元格並從中獲取更新的數據。

如果要單獨更新單元格,可以使用[tableView cellForRowAtIndexPath:]來獲取要更新的單元格。例如

UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; 
    cell.textLabel.text = @"updated text"; 
    // You might need to call the following function though I'm not sure 
    // [cell.textLabel setNeedsDisplay]; 

如果要更新圖像,我建議你添加一個標籤在細胞中的ImageView,然後你就可以改變它,如下所示:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
UIImageView *imageView = [cell viewWithTag:kImageViewTag]; 
imageView.image = updatedImage; 
// Again you might need to tell the image to refresh it self 
//[imageView setNeedsDisplay]; 
0

要改變LabelText的IndexPath上的UITableViewCell,我們可以使用下面的代碼。 - >它工作正常! (這個例子,我改變LabelText的在的tableView的0行):

UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
    cell.textLabel.text = @"updated text"; 
    // must to write line below to update labelText for row 0 
    [tableView setNeedsDisplay];