iPhone_SDK のバックアップ(No.5) - アールメカブ

アールメカブ


iPhone_SDK のバックアップ(No.5)


Programming

iPhone SDK メモ

スタンフォード大で iPhone 開発講座とコードが公開されていたらしいが,今は削除されている.

_ テーブルのグループ化と配列

return [[ array objectAtIndex:section] count]
cex.text =  [[ array objectAtIndex:indexPath.section]
                       objectAtIndex:indexPath.row]
return cell;

でいけるだろう。

こういう方法も紹介されている.

_ 文字列から改行記号を取る.

componentsSeparatedByString関数が指定されたトークンで文字列を区切って配列を返すので,とりあえずこうする.

string = [[string componentsSeparatedByString:@"\n"] 
           objectAtIndex:0];

ただし "\n" は Macintoshではoptions キーを押しながら円マークで入力

_ intValueのエラー処理

int syntaxType = [anObject intValue];
	if (syntaxType == -1) {
		// エラー。例外を発生させる。
		[self release];
		[NSException 
raise:NSInvalidUnarchiveOperationException format:
  @"fail to  decode"];
	}

_ UITableView?にチェックを入れる。

さまざまな情報があって錯綜している。ここにも長い議論があるが、とりあえずはここにあるように使ってみる。

と思ったが,うまくいかないので,チェックしたセクションと行を配列に記憶させ,テーブルがクリックされるたびに [tableView reloadData] で初期化を行うことにした.reloadData 処理には,いろいろ問題もあるようであるが,今のところ,ちゃんと動いている.

Now i have another problem, when i select one and i scroll down. I see that some others are selected to. A bit strange.

You must be using a reuseIdentifier. When you go to return a cell, make sure to set it's accessoryType to the one without the checkmark unless you know it should be checked.

- (NSIndexPath *)tableView:(UITableView *)tableView  
 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	SeismicXMLAppDelegate *appDelegate = 
 (SeismicXMLAppDelegate *)[[UIApplication sharedApplication]
                     delegate];
	[appDelegate showEarthquakeInfo:
            [(TableViewCell *)[tableView 
               cellForRowAtIndexPath:indexPath] quake]];
	[[tableView cellForRowAtIndexPath: oldIndexPath] 
               setAccessoryType:UITableViewCellAccessoryNone];
	[[tableView cellForRowAtIndexPath: indexPath] 
              setAccessoryType:UITableViewCellAccessoryCheckmark];

       oldIndexPath = indexPath;
	return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView 
 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *MyIdentifier = @"MyIdentifier";
	
	TableViewCell *cell = (TableViewCell *)[tableView 
              dequeueReusableCellWithIdentifier:MyIdentifier];
	if(indexPath == oldIndexPath){
		cell.accessoryType = 
                   UITableViewCellAccessoryCheckmark;
	}else{
		cell.accessoryType = UITableViewCellAccessoryNone;

	}
	if (cell == nil) {
		cell = [[[TableViewCell alloc] 
          initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] 
          autorelease];
		
	}
   
   // Set up the cell.
	SeismicXMLAppDelegate *appDelegate = 
(SeismicXMLAppDelegate *)[[UIApplication sharedApplication] 
delegate];
     
   // If the RSS feed isn't accessible (which could happen if
 the network isn't available), show an informative
   // message in the first row of the table.
	if ([appDelegate isDataSourceAvailable] == NO) {
       cell = [[[UITableViewCell alloc] initWithFrame:
         CGRectZero reuseIdentifier:@"DefaultTableViewCell"] 
            autorelease];
       cell.text = NSLocalizedString(@"RSS Host Not Available",
                @"RSS Host Not Available message");
		cell.textColor = [UIColor colorWithWhite:0.5
        alpha:0.5];
		cell.accessoryType = UITableViewCellAccessoryNone;
		return cell;
	}
	
	Earthquake *quakeForRow = [appDelegate 
             objectInListAtIndex:indexPath.row];
   [cell setQuake:quakeForRow];
	return cell;
}
  • あるいは
    - (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	
    	static NSString *SelectionListCellIdentifier = 
              @"SelectionListCellIdentifier";
    	
    	UITableViewCell *cell = [tableView 
       dequeueReusableCellWithIdentifier:SelectionListCellIdentifier];
    	if (cell == nil) {
    		cell = [[[UITableViewCell alloc] 
                        initWithFrame:CGRectZero 
           reuseIdentifier:SelectionListCellIdentifier] autorelease];
    	}
    	
    	NSUInteger row = [indexPath row];
    	NSUInteger oldRow = [lastIndexPath row];
    	cell.text = [list objectAtIndex:row];
    	cell.accessoryType = (row == oldRow && lastIndexPath != 
             nil) ? UITableViewCellAccessoryCheckmark : 
                           UITableViewCellAccessoryNone;
    	
    	return cell;
    }
- (void)tableView:(UITableView *)tableView 
         didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	int newRow = [indexPath row];
	int oldRow = [lastIndexPath row];
	
	if (newRow != oldRow) {
		UITableViewCell *newCell = [tableView 
                     cellForRowAtIndexPath:indexPath];
		newCell.accessoryType = 
                     UITableViewCellAccessoryCheckmark;
		
		UITableViewCell *oldCell = [tableView 
                    cellForRowAtIndexPath: lastIndexPath]; 
		oldCell.accessoryType = 
                      UITableViewCellAccessoryNone;
		
		lastIndexPath = indexPath;  
	}
	
	[tableView deselectRowAtIndexPath:indexPath
                       animated:YES];
}