2015-10-10 76 views
0

enter image description here我收到了一些非常奇怪的行爲與我工作的UITableView。我有以下代碼,它將數據從數組加載到UITableView中。代碼似乎工作...但標籤只顯示值,當我點擊每一行。我難以理解爲什麼會發生這種情況,因爲我很確定我之前沒有遇到過這個問題......但我可能是錯的。我的代碼:UITable單元格信息只顯示何時行被選中

@interface TableViewController() 

@end 

@implementation TableViewController 

//@synthesize cRef; 

-(void) getData:(NSData *) data { 
    NSError *error; 
    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
    [self.tableView reloadData]; 
} 
-(void) start { 
    NSURL *url = [NSURL URLWithString: URL]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    [self getData:data]; 
    } 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self start]; 
    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [json count];; 
} 


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

    static NSString *cellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
    NSDictionary *info = [json objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [info objectForKey:@"CountryName"]; 
    cell.detailTextLabel.text = [info objectForKey:@"id"]; 

    return cell; 
} 

任何幫助表示讚賞。

+0

嘗試更改單元格中標籤的字體顏色。可能是文本顏色與背景顏色匹配。 – KTPATEL

回答

0

正如@ktpatel說嘗試改變字體顏色

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

    static NSString *cellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    if (!cell){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
    } 
    NSDictionary *info = [json objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [info objectForKey:@"CountryName"]; 
    cell.detailTextLabel.text = [info objectForKey:@"id"]; 

    // set font color 
    cell.textLabel.textColor = [UIColor redColor]; 
    cell.detailTextLabel.textColor = [UIColor redColor]; 

    if ([cell isSelected]) { 
     // make changes for selected state 
     cell.textLabel.textColor = [UIColor blackColor]; 
     cell.detailTextLabel.textColor = [UIColor blackColor]; 
    } 
    return cell; 
} 

而且我沒有看到你的didSelectRowAtIndexPath方法的代碼,我想你一定在裏面做這個看的UITableViewCell

的選中狀態
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.detailTextLabel.textColor = [UIColor blackColor]; 
} 
相關問題