gpt4 book ai didi

iphone - 索引在 TableView 中不起作用

转载 作者:行者123 更新时间:2023-11-29 03:42:22 24 4
gpt4 key购买 nike

我正在尝试在表格上实现索引,因为我使用以下方法索引出现在右侧,但它不起作用代码在下面

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

NSArray *toBeReturned = [NSArray arrayWithArray:
[@"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#"
componentsSeparatedByString:@"|"]];

return toBeReturned;
}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

int foundIndex = 0;


for (int i = 0; i< [mainArray count]; i++) {
// Here you return the name i.e. Honda,Mazda
// and match the title for first letter of name
// and move to that row corresponding to that indexpath as below
NSString *letterString = [[[mainArray valueForKey:@"name"] objectAtIndex:i] substringToIndex:1];

NSLog(@"letterString%@",letterString);
NSLog(@"title%@",title);

if ([[letterString uppercaseString ] compare:title] == NSOrderedDescending){
break;
foundIndex++; }

if(foundIndex > mainArray.count)
foundIndex = mainArray.count;
[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:foundIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
return 1;

}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 120;


}
- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//—set the text to display for the cell—

NSLog(@"%d",mainArray.count);
NSLog(@"%@",mainArray);

UIImageView *selectionView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 120)];

UIImage *image = [UIImage imageNamed:@"default.png"];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.layer.cornerRadius = 4;
blockView.layer.masksToBounds = YES;
blockView.layer.borderWidth = 2;
blockView.layer.borderColor = [UIColor blackColor].CGColor;

blockView.frame = CGRectMake(5, 5, 110, 110);

// title isKindOfClass:[NSNull class]])
//NSLog(@"%@",[[mainArray valueForKey:@"image"] objectAtIndex:[indexPath row]]);


id obj = [[mainArray valueForKey:@"image"] objectAtIndex:[indexPath row]];
if ([obj isKindOfClass:[NSString class]])
{

[selectionView addSubview:blockView];
}

else {

UIImage *contactImage = [[UIImage alloc]init];
contactImage = [[mainArray valueForKey:@"image"] objectAtIndex:[indexPath row]];
// NSLog(@" array%@",[[mainArray valueForKey:@"image"] objectAtIndex:[indexPath row]]);
NSLog(@" image%@",contactImage);
UIImageView *blockView = [[UIImageView alloc] initWithImage:contactImage];
blockView.frame = CGRectMake(5, 5, 110, 110);
blockView.layer.cornerRadius = 4;
blockView.layer.masksToBounds = YES;
blockView.layer.borderWidth = 2;
blockView.layer.borderColor = [UIColor blackColor].CGColor;

[selectionView addSubview:blockView];
}

NSLog(@" name :%@",[[mainArray valueForKey:@"name"] objectAtIndex:[indexPath row]]);

if ([[[mainArray valueForKey:@"name"] objectAtIndex:[indexPath row]] isEqualToString:@"(null)"]) {
UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(130, 10, 310, 55)];
name.text =@"Name Not Available";
[name setBackgroundColor:[UIColor clearColor]];
[ name setFont:[UIFont fontWithName:@"Arial-BoldMT" size:30]];
name.textColor = [UIColor whiteColor];
[selectionView addSubview:name];
}
else{

UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(130, 10, 310, 55)];
name.text =[[mainArray valueForKey:@"name"] objectAtIndex:[indexPath row]];
[name setBackgroundColor:[UIColor clearColor]];
[ name setFont:[UIFont fontWithName:@"Arial-BoldMT" size:30]];
name.textColor = [UIColor whiteColor];
[selectionView addSubview:name];
}

if ([[[mainArray valueForKey:@"phone"] objectAtIndex:[indexPath row]] isEqualToString:@"Add Number"]) {

UILabel *phone = [[UILabel alloc]initWithFrame:CGRectMake(130, 70, 300, 50)];
phone.text =@"Not Available";
[phone setBackgroundColor:[UIColor clearColor]];
[ phone setFont:[UIFont fontWithName:@"Arial-BoldMT" size:25]];
phone.textColor = [UIColor whiteColor];
[selectionView addSubview:phone];
}
else{

UILabel *phone = [[UILabel alloc]initWithFrame:CGRectMake(130, 70, 300, 50)];
phone.text =[[mainArray valueForKey:@"phone"] objectAtIndex:[indexPath row]];
[phone setBackgroundColor:[UIColor clearColor]];
[ phone setFont:[UIFont fontWithName:@"Arial-BoldMT" size:25]];
phone.textColor = [UIColor whiteColor];
[selectionView addSubview:phone];

}
if ([[[mainArray valueForKey:@"email"] objectAtIndex:[indexPath row]] isEqualToString:@"Add Email"]) {

UILabel *email = [[UILabel alloc]initWithFrame:CGRectMake(420, 40, 350, 50)];
email.text =@"Email Id Not Available";
[email setBackgroundColor:[UIColor clearColor]];
[ email setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
email.textColor = [UIColor whiteColor];
[selectionView addSubview:email];
}
else{

UILabel *email = [[UILabel alloc]initWithFrame:CGRectMake(420, 40, 350, 50)];
email.text =[[mainArray valueForKey:@"email"] objectAtIndex:[indexPath row]];
[email setBackgroundColor:[UIColor clearColor]];
[ email setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
email.textColor = [UIColor whiteColor];
[selectionView addSubview:email];

}

[[cell contentView] addSubview:selectionView];
return cell;
}

我面临的问题是,当我单击任何索引而不是正确的索引时,它会到达表格顶部

最佳答案

要使用索引,您的表实际上应该分为几个部分。目前您似乎要求框架滚动到指定行,然后立即滚动到第 0 节的顶部。

如果您使用performSelector在短暂延迟后请求滚动(可能看起来很糟糕),它可能会起作用。或者,如果您从该方法返回 -1(文档不支持这种猜测)。

答案实际上是使用表中的部分并让框架按设计工作。

关于iphone - 索引在 TableView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18223472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com