tableview点击变cell背景颜色问题

jcskey 2014-05-15 03:06:20
功能就是多选效果,点击其中一个 背景变色,在点击就回变回白色,求教
1,一共18个数组,点击第一个cell改变颜色的时候第15个也自动变更颜色,第二个16个也回同事变色,第三个17个也回,
代码如下
[code=objc]@interface fivetableview ()

@end

@implementation fivetableview
@synthesize dlists;
@synthesize dellist;

NSInteger i;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{

[super viewDidLoad];
dellist=[[NSMutableArray alloc]initWithCapacity:30];
dlists=[[NSMutableArray alloc] initWithCapacity:30 ];
[dlists addObject:@"大众化的"];
[dlists addObject:@"线条硬朗的"];
[dlists addObject:@"流线型的"];
[dlists addObject:@"大气的"];
[dlists addObject:@"时尚的"];
[dlists addObject:@"简约的"];
[dlists addObject:@"有力量感的"];
[dlists addObject:@"张扬的"];
[dlists addObject:@"霸气的"];
[dlists addObject:@"动感的"];
[dlists addObject:@"稳重的"];
[dlists addObject:@"优雅的"];
[dlists addObject:@"粗犷的"];
[dlists addObject:@"创新的"];
[dlists addObject:@"中性的"];
[dlists addObject:@"活力的"];
[dlists addObject:@"经典的"];
NSLog(@"%d",[dlists count]);
- (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 [dlists count];
//return [dlists count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSInteger row = [indexPath row];
cell.textLabel.text = [self.dlists objectAtIndex:row];
NSLog(@"row=%d",row);
return cell;
}


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


UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];

@end
[/code]
...全文
2650 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ixcoder 2014-05-19
  • 打赏
  • 举报
回复
每个style 有唯一编号, 选中其中1个时将其颜色属性assgin value,cell显示得时候取出来判断什么颜色,再显示之
jcskey 2014-05-15
  • 打赏
  • 举报
回复
引用 6 楼 ixcoder 的回复:
貌似你得data model 需要增加1个颜色得属性,也许是个数字什么,要把颜色属性记录下来 class style { id: color: description: } 10 个styles 。。。。 u know ?
不太清楚请详细讲解下谢谢 。我就不知道为什么我点击第一个每15个cell都会变成红色。
ixcoder 2014-05-15
  • 打赏
  • 举报
回复
貌似你得data model 需要增加1个颜色得属性,也许是个数字什么,要把颜色属性记录下来 class style { id: color: description: } 10 个styles 。。。。 u know ?
jcskey 2014-05-15
  • 打赏
  • 举报
回复
cell.sel方法貌似不行。
不担心 2014-05-15
  • 打赏
  • 举报
回复
我觉得正常的做法应该是你在didSelectRowAtIndexPath这个方法中设置cell.selected=!cell.selected 然后在cellForRowAtIndexPath方法中根据cell的selected值上不同的颜色
不担心 2014-05-15
  • 打赏
  • 举报
回复
哦,没注意你是要多选 自己去实现一个uitableviewCell的子类,创建的时候默认就会给你继续选择的方法,在那边实现选中的功能可能比较容易 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state }
jcskey 2014-05-15
  • 打赏
  • 举报
回复
那就补能多选了。
不担心 2014-05-15
  • 打赏
  • 举报
回复
估计是重用的问题 实现 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 这个方法,把没选中的颜色改回原来的

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧