我们如何通过默认选择tableList行

时间:2016-02-10 12:50:00

标签: ios objective-c uitableview

嗨,我是ios的新手,在我创建的应用程序中 一个UItablaList好了

这里默认我的tableList行一个textcolor 我已经指定了“RED”

当我点击另一行时,第一行颜色必须为“WHITE”,所选行颜色必须为“RED”,如果我点击dy默认选择第一行,则该颜色必须为“RED,请帮助我一些,我们如何为此

实现逻辑

我的代码: -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     static NSString *simpleTableIdentifier = @"MyCell";
    MyCellTableViewCell *cell = (MyCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCellTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    UILabel *lineLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 55, RightMenuTbl.frame.size.width, 1)];
    lineLbl.backgroundColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:lineLbl];

    //remove background for table:-

   if (indexPath.row == 0) {
      lineLbl.textColor = [UIColor redColor];
    }
 }

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

}

3 个答案:

答案 0 :(得分:1)

在.h文件和didselectrow

中将int作为selectedRow
selectedRow = indexPath.row;
[yourtableview reloadData];

和在cellforrow中

if (selectedRow== indexPath.row) {
    lineLbl.textColor = set color you want for selected
}else
    lineLbl.textColor = set color you want for not 

选择

答案 1 :(得分:0)

试试这段代码:

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

            NSIndexPath * cellIndexPath = indexPath;

            SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:cellIndexPath];


             cell.nameLbl.textColor = [UIColor redColor];


        }

    (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

    SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath]; 
cell.nameLbl.textColor = [UIColor whiteColor];

    }

答案 2 :(得分:0)

请尝试以下代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.nameLbl.textColor = [UIColor redColor];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.nameLbl.textColor = [UIColor whiteColor];
}