limon
limon

Reputation: 3212

UITableViewCellSelection style doesn't properly work

I'm developing an app using Xcode 4.5 and iOS 5.0.

In my app, i have a UITableView called surveyTableView created through Storyboard. I want my surveyTableView cells selected and i'm using this piece of code to make it happen.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
        {
            static NSString *cellIdentifier = @"Cell Identifier";
            UITableViewCell *cell = [self.surveyTableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if(!cell)
            {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            cell.textLabel.text = [[self.surveyInfoArray objectAtIndex:indexPath.row] valueForKey:@"SurveyName"];
            [cell setSelected:YES];
            return  cell ;
        }

I've only one section whose number of rows is 2 in this case. Here is the image what i got on the screen. Screenshot

Do you guys any idea why this is happening ?

EDIT : I've set UITableView selection attribute to Multiple Selection, even if it's Single Line Selection, i realized that there is no difference.

EDIT 2 : Here,i get image when ,i select UITableViewCellStyleSubtitleScreenshot2

EDIT 3 : Here is the interesting part, when i select right now, here is the image.That's driving me crazyScreenshot3

Upvotes: 0

Views: 186

Answers (5)

Arun
Arun

Reputation: 590

I think you are not able to fetch the data from surveyTableView

NSString *cellValue= [[self.surveyInfoArray objectAtIndex:indexPath.row] valueForKey:@"SurveyName"];
NSLog(@"%@",cellValue);

check whether it's printing some values in console or not

Upvotes: 0

user2185887
user2185887

Reputation:

please remove this [cell setSelected:YES];

Upvotes: 4

Balu
Balu

Reputation: 8460

for that You should use selectRowAtIndexPath:animated:scrollPosition: instead selected property for UITableViewCell in cellForRowAtIndexPath.

by using this one you can select a cell always , and only change when you select an another cell

Upvotes: 1

Suhaiyl
Suhaiyl

Reputation: 1081

I also had same problem, fixed it via setting attributes properly in storyboard. Attaching the screenshot of tableview properties. Hope this helps you

enter image description here

Upvotes: 0

Kasaname
Kasaname

Reputation: 1501

if(!cell)
            {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            }

Upvotes: 0

Related Questions